2 * Support code for the Common UNIX Printing System ("CUPS")
4 * Copyright 1999-2003 by Michael R Sweet.
5 * Copyright 2008 Jeremy Allison.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
22 * JRA. Converted to utf8 pull/push.
27 #include "printing/pcap.h"
28 #include "librpc/gen_ndr/ndr_printcap.h"
29 #include "lib/util/sys_rw.h"
32 #include <cups/cups.h>
33 #include <cups/language.h>
34 #include <cups/http.h>
36 /* CUPS prior to version 1.7 doesn't have HTTP_URI_STATUS_OK */
37 #if (CUPS_VERSION_MAJOR == 1) && (CUPS_VERSION_MINOR < 7)
38 #define HTTP_URI_STATUS_OK HTTP_URI_OK
41 #if (CUPS_VERSION_MAJOR > 1) || (CUPS_VERSION_MINOR > 5)
42 #define HAVE_CUPS_1_6 1
46 #define ippGetGroupTag(attr) attr->group_tag
47 #define ippGetName(attr) attr->name
48 #define ippGetValueTag(attr) attr->value_tag
49 #define ippGetStatusCode(ipp) ipp->request.status.status_code
50 #define ippGetInteger(attr, element) attr->values[element].integer
51 #define ippGetString(attr, element, language) attr->values[element].string.text
53 static ipp_attribute_t
*
54 ippFirstAttribute(ipp_t
*ipp
)
58 return (ipp
->current
= ipp
->attrs
);
61 static ipp_attribute_t
*
62 ippNextAttribute(ipp_t
*ipp
)
64 if (!ipp
|| !ipp
->current
)
66 return (ipp
->current
= ipp
->current
->next
);
69 static int ippSetOperation(ipp_t
*ipp
, ipp_op_t op
)
71 ipp
->request
.op
.operation_id
= op
;
75 static int ippSetRequestId(ipp_t
*ipp
, int request_id
)
77 ipp
->request
.any
.request_id
= request_id
;
82 static SIG_ATOMIC_T gotalarm
;
84 /***************************************************************
85 Signal function to tell us we timed out.
86 ****************************************************************/
88 static void gotalarm_sig(int signum
)
93 extern userdom_struct current_user_info
;
96 * 'cups_passwd_cb()' - The CUPS password callback...
99 static const char * /* O - Password or NULL */
100 cups_passwd_cb(const char *prompt
) /* I - Prompt */
103 * Always return NULL to indicate that no password is available...
109 static http_t
*cups_connect(TALLOC_CTX
*frame
)
112 char *server
= NULL
, *p
= NULL
;
114 int timeout
= lp_cups_connection_timeout();
117 if (lp_cups_server(talloc_tos()) != NULL
&& strlen(lp_cups_server(talloc_tos())) > 0) {
118 if (!push_utf8_talloc(frame
, &server
, lp_cups_server(talloc_tos()), &size
)) {
122 server
= talloc_strdup(frame
,cupsServer());
128 p
= strchr(server
, ':');
136 DEBUG(10, ("connecting to cups server %s:%d\n",
142 CatchSignal(SIGALRM
, gotalarm_sig
);
146 #ifdef HAVE_HTTPCONNECTENCRYPT
147 http
= httpConnectEncrypt(server
, port
, lp_cups_encrypt());
149 http
= httpConnect(server
, port
);
153 CatchSignal(SIGALRM
, SIG_IGN
);
157 DEBUG(3,("Unable to connect to CUPS server %s:%d - %s\n",
158 server
, port
, strerror(errno
)));
164 static bool send_pcap_blob(DATA_BLOB
*pcap_blob
, int fd
)
168 ret
= sys_write(fd
, &pcap_blob
->length
, sizeof(pcap_blob
->length
));
169 if (ret
!= sizeof(pcap_blob
->length
)) {
173 ret
= sys_write(fd
, pcap_blob
->data
, pcap_blob
->length
);
174 if (ret
!= pcap_blob
->length
) {
178 DEBUG(10, ("successfully sent blob of len %d\n", (int)ret
));
182 static bool recv_pcap_blob(TALLOC_CTX
*mem_ctx
, int fd
, DATA_BLOB
*pcap_blob
)
187 ret
= sys_read(fd
, &blob_len
, sizeof(blob_len
));
188 if (ret
!= sizeof(blob_len
)) {
192 *pcap_blob
= data_blob_talloc_named(mem_ctx
, NULL
, blob_len
,
194 if (pcap_blob
->length
!= blob_len
) {
197 ret
= sys_read(fd
, pcap_blob
->data
, blob_len
);
198 if (ret
!= blob_len
) {
199 talloc_free(pcap_blob
->data
);
203 DEBUG(10, ("successfully recvd blob of len %d\n", (int)ret
));
207 static bool process_cups_printers_response(TALLOC_CTX
*mem_ctx
,
209 struct pcap_data
*pcap_data
)
211 ipp_attribute_t
*attr
;
214 char *location
= NULL
;
215 struct pcap_printer
*printer
;
218 for (attr
= ippFirstAttribute(response
); attr
!= NULL
;) {
220 * Skip leading attributes until we hit a printer...
223 while (attr
!= NULL
&& ippGetGroupTag(attr
) != IPP_TAG_PRINTER
)
224 attr
= ippNextAttribute(response
);
230 * Pull the needed attributes from this printer...
236 while (attr
!= NULL
&& ippGetGroupTag(attr
) == IPP_TAG_PRINTER
) {
238 if (strcmp(ippGetName(attr
), "printer-name") == 0 &&
239 ippGetValueTag(attr
) == IPP_TAG_NAME
) {
240 if (!pull_utf8_talloc(mem_ctx
,
242 ippGetString(attr
, 0, NULL
),
248 if (strcmp(ippGetName(attr
), "printer-info") == 0 &&
249 ippGetValueTag(attr
) == IPP_TAG_TEXT
) {
250 if (!pull_utf8_talloc(mem_ctx
,
252 ippGetString(attr
, 0, NULL
),
258 if (strcmp(ippGetName(attr
), "printer-location") == 0 &&
259 ippGetValueTag(attr
) == IPP_TAG_TEXT
) {
260 if (!pull_utf8_talloc(mem_ctx
,
262 ippGetString(attr
, 0, NULL
),
268 attr
= ippNextAttribute(response
);
272 * See if we have everything needed...
278 if (pcap_data
->count
== 0) {
279 printer
= talloc_array(mem_ctx
, struct pcap_printer
, 1);
281 printer
= talloc_realloc(mem_ctx
, pcap_data
->printers
,
283 pcap_data
->count
+ 1);
285 if (printer
== NULL
) {
288 pcap_data
->printers
= printer
;
289 pcap_data
->printers
[pcap_data
->count
].name
= name
;
290 pcap_data
->printers
[pcap_data
->count
].info
= info
;
291 pcap_data
->printers
[pcap_data
->count
].location
= location
;
301 * request printer list from cups, send result back to up parent via fd.
302 * returns true if the (possibly failed) result was successfully sent to parent.
304 static bool cups_cache_reload_async(int fd
)
306 TALLOC_CTX
*frame
= talloc_stackframe();
307 struct pcap_data pcap_data
;
308 http_t
*http
= NULL
; /* HTTP connection to server */
309 ipp_t
*request
= NULL
, /* IPP Request */
310 *response
= NULL
; /* IPP Response */
311 cups_lang_t
*language
= NULL
; /* Default language */
312 static const char *requested
[] =/* Requested attributes */
319 enum ndr_err_code ndr_ret
;
322 ZERO_STRUCT(pcap_data
);
323 pcap_data
.status
= NT_STATUS_UNSUCCESSFUL
;
325 DEBUG(5, ("reloading cups printcap cache\n"));
328 * Make sure we don't ask for passwords...
331 cupsSetPasswordCB(cups_passwd_cb
);
333 if ((http
= cups_connect(frame
)) == NULL
) {
338 * Build a CUPS_GET_PRINTERS request, which requires the following
342 * attributes-natural-language
343 * requested-attributes
348 ippSetOperation(request
, CUPS_GET_PRINTERS
);
349 ippSetRequestId(request
, 1);
351 language
= cupsLangDefault();
353 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
354 "attributes-charset", NULL
, "utf-8");
356 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
357 "attributes-natural-language", NULL
, language
->language
);
359 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
360 "requested-attributes",
361 (sizeof(requested
) / sizeof(requested
[0])),
364 if ((response
= cupsDoRequest(http
, request
, "/")) == NULL
) {
365 DEBUG(0,("Unable to get printer list - %s\n",
366 ippErrorString(cupsLastError())));
370 ret
= process_cups_printers_response(frame
, response
, &pcap_data
);
372 DEBUG(0,("failed to process cups response\n"));
380 * Build a CUPS_GET_CLASSES request, which requires the following
384 * attributes-natural-language
385 * requested-attributes
390 ippSetOperation(request
, CUPS_GET_CLASSES
);
391 ippSetRequestId(request
, 1);
393 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
394 "attributes-charset", NULL
, "utf-8");
396 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
397 "attributes-natural-language", NULL
, language
->language
);
399 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
400 "requested-attributes",
401 (sizeof(requested
) / sizeof(requested
[0])),
404 if ((response
= cupsDoRequest(http
, request
, "/")) == NULL
) {
405 DEBUG(0,("Unable to get printer list - %s\n",
406 ippErrorString(cupsLastError())));
410 ret
= process_cups_printers_response(frame
, response
, &pcap_data
);
412 DEBUG(0,("failed to process cups response\n"));
416 pcap_data
.status
= NT_STATUS_OK
;
422 cupsLangFree(language
);
428 ndr_ret
= ndr_push_struct_blob(&pcap_blob
, frame
, &pcap_data
,
429 (ndr_push_flags_fn_t
)ndr_push_pcap_data
);
430 if (ndr_ret
== NDR_ERR_SUCCESS
) {
431 ret
= send_pcap_blob(&pcap_blob
, fd
);
438 static struct tevent_fd
*cache_fd_event
;
440 static bool cups_pcap_load_async(struct tevent_context
*ev
,
441 struct messaging_context
*msg_ctx
,
450 if (cache_fd_event
) {
451 DEBUG(3,("cups_pcap_load_async: already waiting for "
452 "a refresh event\n" ));
456 DEBUG(5,("cups_pcap_load_async: asynchronously loading cups printers\n"));
458 if (pipe(fds
) == -1) {
463 if (pid
== (pid_t
)-1) {
464 DEBUG(10,("cups_pcap_load_async: fork failed %s\n",
472 DEBUG(10,("cups_pcap_load_async: child pid = %u\n",
473 (unsigned int)pid
));
482 close_all_print_db();
484 status
= reinit_after_fork(msg_ctx
, ev
, true, NULL
);
485 if (!NT_STATUS_IS_OK(status
)) {
486 DEBUG(0,("cups_pcap_load_async: reinit_after_fork() failed\n"));
487 smb_panic("cups_pcap_load_async: reinit_after_fork() failed");
491 cups_cache_reload_async(fds
[1]);
493 TALLOC_FREE(msg_ctx
);
497 struct cups_async_cb_args
{
499 struct tevent_context
*event_ctx
;
500 struct messaging_context
*msg_ctx
;
501 void (*post_cache_fill_fn
)(struct tevent_context
*,
502 struct messaging_context
*);
505 static void cups_async_callback(struct tevent_context
*event_ctx
,
506 struct tevent_fd
*event
,
510 TALLOC_CTX
*frame
= talloc_stackframe();
511 struct cups_async_cb_args
*cb_args
= (struct cups_async_cb_args
*)p
;
512 struct pcap_cache
*tmp_pcap_cache
= NULL
;
514 struct pcap_data pcap_data
;
516 enum ndr_err_code ndr_ret
;
519 DEBUG(5,("cups_async_callback: callback received for printer data. "
520 "fd = %d\n", cb_args
->pipe_fd
));
522 ret_ok
= recv_pcap_blob(frame
, cb_args
->pipe_fd
, &pcap_blob
);
524 DEBUG(0,("failed to recv pcap blob\n"));
528 ndr_ret
= ndr_pull_struct_blob(&pcap_blob
, frame
, &pcap_data
,
529 (ndr_pull_flags_fn_t
)ndr_pull_pcap_data
);
530 if (ndr_ret
!= NDR_ERR_SUCCESS
) {
534 if (!NT_STATUS_IS_OK(pcap_data
.status
)) {
535 DEBUG(3,("failed to retrieve printer list: %s\n",
536 nt_errstr(pcap_data
.status
)));
540 for (i
= 0; i
< pcap_data
.count
; i
++) {
541 ret_ok
= pcap_cache_add_specific(&tmp_pcap_cache
,
542 pcap_data
.printers
[i
].name
,
543 pcap_data
.printers
[i
].info
,
544 pcap_data
.printers
[i
].location
);
546 DEBUG(0, ("failed to add to tmp pcap cache\n"));
551 /* replace the system-wide pcap cache with a (possibly empty) new one */
552 ret_ok
= pcap_cache_replace(tmp_pcap_cache
);
554 DEBUG(0, ("failed to replace pcap cache\n"));
555 } else if (cb_args
->post_cache_fill_fn
!= NULL
) {
556 /* Caller requested post cache fill callback */
557 cb_args
->post_cache_fill_fn(cb_args
->event_ctx
,
561 pcap_cache_destroy_specific(&tmp_pcap_cache
);
563 close(cb_args
->pipe_fd
);
564 TALLOC_FREE(cb_args
);
565 TALLOC_FREE(cache_fd_event
);
568 bool cups_cache_reload(struct tevent_context
*ev
,
569 struct messaging_context
*msg_ctx
,
570 void (*post_cache_fill_fn
)(struct tevent_context
*,
571 struct messaging_context
*))
573 struct cups_async_cb_args
*cb_args
;
576 cb_args
= talloc(NULL
, struct cups_async_cb_args
);
577 if (cb_args
== NULL
) {
581 cb_args
->post_cache_fill_fn
= post_cache_fill_fn
;
582 cb_args
->event_ctx
= ev
;
583 cb_args
->msg_ctx
= msg_ctx
;
584 p_pipe_fd
= &cb_args
->pipe_fd
;
587 /* Set up an async refresh. */
588 if (!cups_pcap_load_async(ev
, msg_ctx
, p_pipe_fd
)) {
589 talloc_free(cb_args
);
593 DEBUG(10,("cups_cache_reload: async read on fd %d\n",
596 /* Trigger an event when the pipe can be read. */
597 cache_fd_event
= tevent_add_fd(ev
,
602 if (!cache_fd_event
) {
604 TALLOC_FREE(cb_args
);
612 * 'cups_job_delete()' - Delete a job.
615 static int cups_job_delete(const char *sharename
, const char *lprm_command
, struct printjob
*pjob
)
617 TALLOC_CTX
*frame
= talloc_stackframe();
618 int ret
= 1; /* Return value */
619 http_t
*http
= NULL
; /* HTTP connection to server */
620 ipp_t
*request
= NULL
, /* IPP Request */
621 *response
= NULL
; /* IPP Response */
622 cups_lang_t
*language
= NULL
; /* Default language */
624 char uri
[HTTP_MAX_URI
] = {0}; /* printer-uri attribute */
625 http_uri_status_t ustatus
;
628 DEBUG(5,("cups_job_delete(%s, %p (%d))\n", sharename
, pjob
, pjob
->sysjob
));
631 * Make sure we don't ask for passwords...
634 cupsSetPasswordCB(cups_passwd_cb
);
637 * Try to connect to the server...
640 if ((http
= cups_connect(frame
)) == NULL
) {
645 * Build an IPP_CANCEL_JOB request, which requires the following
649 * attributes-natural-language
651 * requesting-user-name
656 ippSetOperation(request
, IPP_CANCEL_JOB
);
657 ippSetRequestId(request
, 1);
659 language
= cupsLangDefault();
661 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
662 "attributes-charset", NULL
, "utf-8");
664 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
665 "attributes-natural-language", NULL
, language
->language
);
667 ustatus
= httpAssembleURIf(HTTP_URI_CODING_ALL
,
676 if (ustatus
!= HTTP_URI_STATUS_OK
) {
680 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
682 if (!push_utf8_talloc(frame
, &user
, pjob
->user
, &size
)) {
686 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
690 * Do the request and get back a response...
693 if ((response
= cupsDoRequest(http
, request
, "/jobs")) != NULL
) {
694 if (ippGetStatusCode(response
) >= IPP_OK_CONFLICT
) {
695 DEBUG(0,("Unable to cancel job %d - %s\n", pjob
->sysjob
,
696 ippErrorString(cupsLastError())));
701 DEBUG(0,("Unable to cancel job %d - %s\n", pjob
->sysjob
,
702 ippErrorString(cupsLastError())));
710 cupsLangFree(language
);
721 * 'cups_job_pause()' - Pause a job.
724 static int cups_job_pause(int snum
, struct printjob
*pjob
)
726 TALLOC_CTX
*frame
= talloc_stackframe();
727 int ret
= 1; /* Return value */
728 http_t
*http
= NULL
; /* HTTP connection to server */
729 ipp_t
*request
= NULL
, /* IPP Request */
730 *response
= NULL
; /* IPP Response */
731 cups_lang_t
*language
= NULL
; /* Default language */
733 char uri
[HTTP_MAX_URI
] = {0}; /* printer-uri attribute */
734 http_uri_status_t ustatus
;
737 DEBUG(5,("cups_job_pause(%d, %p (%d))\n", snum
, pjob
, pjob
->sysjob
));
740 * Make sure we don't ask for passwords...
743 cupsSetPasswordCB(cups_passwd_cb
);
746 * Try to connect to the server...
749 if ((http
= cups_connect(frame
)) == NULL
) {
754 * Build an IPP_HOLD_JOB request, which requires the following
758 * attributes-natural-language
760 * requesting-user-name
765 ippSetOperation(request
, IPP_HOLD_JOB
);
766 ippSetRequestId(request
, 1);
768 language
= cupsLangDefault();
770 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
771 "attributes-charset", NULL
, "utf-8");
773 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
774 "attributes-natural-language", NULL
, language
->language
);
776 ustatus
= httpAssembleURIf(HTTP_URI_CODING_ALL
,
785 if (ustatus
!= HTTP_URI_STATUS_OK
) {
789 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
791 if (!push_utf8_talloc(frame
, &user
, pjob
->user
, &size
)) {
794 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
798 * Do the request and get back a response...
801 if ((response
= cupsDoRequest(http
, request
, "/jobs")) != NULL
) {
802 if (ippGetStatusCode(response
) >= IPP_OK_CONFLICT
) {
803 DEBUG(0,("Unable to hold job %d - %s\n", pjob
->sysjob
,
804 ippErrorString(cupsLastError())));
809 DEBUG(0,("Unable to hold job %d - %s\n", pjob
->sysjob
,
810 ippErrorString(cupsLastError())));
818 cupsLangFree(language
);
829 * 'cups_job_resume()' - Resume a paused job.
832 static int cups_job_resume(int snum
, struct printjob
*pjob
)
834 TALLOC_CTX
*frame
= talloc_stackframe();
835 int ret
= 1; /* Return value */
836 http_t
*http
= NULL
; /* HTTP connection to server */
837 ipp_t
*request
= NULL
, /* IPP Request */
838 *response
= NULL
; /* IPP Response */
839 cups_lang_t
*language
= NULL
; /* Default language */
841 char uri
[HTTP_MAX_URI
] = {0}; /* printer-uri attribute */
842 http_uri_status_t ustatus
;
845 DEBUG(5,("cups_job_resume(%d, %p (%d))\n", snum
, pjob
, pjob
->sysjob
));
848 * Make sure we don't ask for passwords...
851 cupsSetPasswordCB(cups_passwd_cb
);
854 * Try to connect to the server...
857 if ((http
= cups_connect(frame
)) == NULL
) {
862 * Build an IPP_RELEASE_JOB request, which requires the following
866 * attributes-natural-language
868 * requesting-user-name
873 ippSetOperation(request
, IPP_RELEASE_JOB
);
874 ippSetRequestId(request
, 1);
876 language
= cupsLangDefault();
878 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
879 "attributes-charset", NULL
, "utf-8");
881 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
882 "attributes-natural-language", NULL
, language
->language
);
884 ustatus
= httpAssembleURIf(HTTP_URI_CODING_ALL
,
893 if (ustatus
!= HTTP_URI_STATUS_OK
) {
897 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
899 if (!push_utf8_talloc(frame
, &user
, pjob
->user
, &size
)) {
902 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
906 * Do the request and get back a response...
909 if ((response
= cupsDoRequest(http
, request
, "/jobs")) != NULL
) {
910 if (ippGetStatusCode(response
) >= IPP_OK_CONFLICT
) {
911 DEBUG(0,("Unable to release job %d - %s\n", pjob
->sysjob
,
912 ippErrorString(cupsLastError())));
917 DEBUG(0,("Unable to release job %d - %s\n", pjob
->sysjob
,
918 ippErrorString(cupsLastError())));
926 cupsLangFree(language
);
937 * 'cups_job_submit()' - Submit a job for printing.
940 static int cups_job_submit(int snum
, struct printjob
*pjob
,
941 enum printing_types printing_type
,
944 TALLOC_CTX
*frame
= talloc_stackframe();
945 int ret
= 1; /* Return value */
946 http_t
*http
= NULL
; /* HTTP connection to server */
947 ipp_t
*request
= NULL
, /* IPP Request */
948 *response
= NULL
; /* IPP Response */
949 ipp_attribute_t
*attr_job_id
= NULL
; /* IPP Attribute "job-id" */
950 cups_lang_t
*language
= NULL
; /* Default language */
951 char uri
[HTTP_MAX_URI
] = {0}; /* printer-uri attribute */
952 http_uri_status_t ustatus
;
953 char *new_jobname
= NULL
;
955 cups_option_t
*options
= NULL
;
956 char *printername
= NULL
;
958 char *jobname
= NULL
;
959 char *cupsoptions
= NULL
;
960 char *filename
= NULL
;
963 DEBUG(5,("cups_job_submit(%d, %p)\n", snum
, pjob
));
966 * Make sure we don't ask for passwords...
969 cupsSetPasswordCB(cups_passwd_cb
);
972 * Try to connect to the server...
975 if ((http
= cups_connect(frame
)) == NULL
) {
980 * Build an IPP_PRINT_JOB request, which requires the following
984 * attributes-natural-language
986 * requesting-user-name
992 ippSetOperation(request
, IPP_PRINT_JOB
);
993 ippSetRequestId(request
, 1);
995 language
= cupsLangDefault();
997 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
998 "attributes-charset", NULL
, "utf-8");
1000 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1001 "attributes-natural-language", NULL
, language
->language
);
1003 if (!push_utf8_talloc(frame
, &printername
,
1004 lp_printername(talloc_tos(), snum
),
1008 ustatus
= httpAssembleURIf(HTTP_URI_CODING_ALL
,
1012 NULL
, /* username */
1017 if (ustatus
!= HTTP_URI_STATUS_OK
) {
1021 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1022 "printer-uri", NULL
, uri
);
1024 if (!push_utf8_talloc(frame
, &user
, pjob
->user
, &size
)) {
1027 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
1030 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1031 "job-originating-host-name", NULL
,
1032 pjob
->clientmachine
);
1034 if (!push_utf8_talloc(frame
, &jobname
, pjob
->jobname
, &size
)) {
1037 new_jobname
= talloc_asprintf(frame
,
1038 "%s%.8u %s", PRINT_SPOOL_PREFIX
,
1039 pjob
->jobid
, jobname
);
1040 if (new_jobname
== NULL
) {
1044 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name", NULL
,
1048 * add any options defined in smb.conf
1051 if (!push_utf8_talloc(frame
, &cupsoptions
,
1052 lp_cups_options(talloc_tos(), snum
), &size
)) {
1057 num_options
= cupsParseOptions(cupsoptions
, num_options
, &options
);
1060 cupsEncodeOptions(request
, num_options
, options
);
1063 * Do the request and get back a response...
1066 ustatus
= httpAssembleURIf(HTTP_URI_CODING_ALL
,
1070 NULL
, /* username */
1075 if (ustatus
!= HTTP_URI_STATUS_OK
) {
1079 if (!push_utf8_talloc(frame
, &filename
, pjob
->filename
, &size
)) {
1082 if ((response
= cupsDoFileRequest(http
, request
, uri
, pjob
->filename
)) != NULL
) {
1083 if (ippGetStatusCode(response
) >= IPP_OK_CONFLICT
) {
1084 DEBUG(0,("Unable to print file to %s - %s\n",
1085 lp_printername(talloc_tos(), snum
),
1086 ippErrorString(cupsLastError())));
1089 attr_job_id
= ippFindAttribute(response
, "job-id", IPP_TAG_INTEGER
);
1091 pjob
->sysjob
= ippGetInteger(attr_job_id
, 0);
1092 DEBUG(5,("cups_job_submit: job-id %d\n", pjob
->sysjob
));
1094 DEBUG(0,("Missing job-id attribute in IPP response"));
1098 DEBUG(0,("Unable to print file to `%s' - %s\n",
1099 lp_printername(talloc_tos(), snum
),
1100 ippErrorString(cupsLastError())));
1104 unlink(pjob
->filename
);
1105 /* else print_job_end will do it for us */
1109 ippDelete(response
);
1112 cupsLangFree(language
);
1123 * 'cups_queue_get()' - Get all the jobs in the print queue.
1126 static int cups_queue_get(const char *sharename
,
1127 enum printing_types printing_type
,
1129 print_queue_struct
**q
,
1130 print_status_struct
*status
)
1132 TALLOC_CTX
*frame
= talloc_stackframe();
1133 char *printername
= NULL
;
1134 http_t
*http
= NULL
; /* HTTP connection to server */
1135 ipp_t
*request
= NULL
, /* IPP Request */
1136 *response
= NULL
; /* IPP Response */
1137 ipp_attribute_t
*attr
= NULL
; /* Current attribute */
1138 cups_lang_t
*language
= NULL
; /* Default language */
1139 char uri
[HTTP_MAX_URI
] = {0}; /* printer-uri attribute */
1140 http_uri_status_t ustatus
;
1141 int qcount
= 0, /* Number of active queue entries */
1142 qalloc
= 0; /* Number of queue entries allocated */
1143 print_queue_struct
*queue
= NULL
, /* Queue entries */
1144 *temp
; /* Temporary pointer for queue */
1145 char *user_name
= NULL
, /* job-originating-user-name attribute */
1146 *job_name
= NULL
; /* job-name attribute */
1147 int job_id
; /* job-id attribute */
1148 int job_k_octets
; /* job-k-octets attribute */
1149 time_t job_time
; /* time-at-creation attribute */
1150 ipp_jstate_t job_status
; /* job-status attribute */
1151 int job_priority
; /* job-priority attribute */
1153 static const char *jattrs
[] = /* Requested job attributes */
1158 "job-originating-user-name",
1163 static const char *pattrs
[] = /* Requested printer attributes */
1166 "printer-state-message"
1171 /* HACK ALERT!!! The problem with support the 'printer name'
1172 option is that we key the tdb off the sharename. So we will
1173 overload the lpq_command string to pass in the printername
1174 (which is basically what we do for non-cups printers ... using
1175 the lpq_command to get the queue listing). */
1177 if (!push_utf8_talloc(frame
, &printername
, lpq_command
, &size
)) {
1180 DEBUG(5,("cups_queue_get(%s, %p, %p)\n", lpq_command
, q
, status
));
1183 * Make sure we don't ask for passwords...
1186 cupsSetPasswordCB(cups_passwd_cb
);
1189 * Try to connect to the server...
1192 if ((http
= cups_connect(frame
)) == NULL
) {
1197 * Generate the printer URI...
1200 ustatus
= httpAssembleURIf(HTTP_URI_CODING_ALL
,
1204 NULL
, /* username */
1209 if (ustatus
!= HTTP_URI_STATUS_OK
) {
1214 * Build an IPP_GET_JOBS request, which requires the following
1217 * attributes-charset
1218 * attributes-natural-language
1219 * requested-attributes
1225 ippSetOperation(request
, IPP_GET_JOBS
);
1226 ippSetRequestId(request
, 1);
1228 language
= cupsLangDefault();
1230 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1231 "attributes-charset", NULL
, "utf-8");
1233 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1234 "attributes-natural-language", NULL
, language
->language
);
1236 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1237 "requested-attributes",
1238 (sizeof(jattrs
) / sizeof(jattrs
[0])),
1241 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1242 "printer-uri", NULL
, uri
);
1245 * Do the request and get back a response...
1248 if ((response
= cupsDoRequest(http
, request
, "/")) == NULL
) {
1249 DEBUG(0,("Unable to get jobs for %s - %s\n", uri
,
1250 ippErrorString(cupsLastError())));
1254 if (ippGetStatusCode(response
) >= IPP_OK_CONFLICT
) {
1255 DEBUG(0,("Unable to get jobs for %s - %s\n", uri
,
1256 ippErrorString(ippGetStatusCode(response
))));
1261 * Process the jobs...
1268 for (attr
= ippFirstAttribute(response
); attr
!= NULL
; attr
= ippNextAttribute(response
)) {
1270 * Skip leading attributes until we hit a job...
1273 while (attr
!= NULL
&& ippGetGroupTag(attr
) != IPP_TAG_JOB
)
1274 attr
= ippNextAttribute(response
);
1280 * Allocate memory as needed...
1282 if (qcount
>= qalloc
) {
1285 queue
= SMB_REALLOC_ARRAY(queue
, print_queue_struct
, qalloc
);
1287 if (queue
== NULL
) {
1288 DEBUG(0,("cups_queue_get: Not enough memory!"));
1294 temp
= queue
+ qcount
;
1295 memset(temp
, 0, sizeof(print_queue_struct
));
1298 * Pull the needed attributes from this job...
1303 job_status
= IPP_JOB_PENDING
;
1309 while (attr
!= NULL
&& ippGetGroupTag(attr
) == IPP_TAG_JOB
) {
1310 if (ippGetName(attr
) == NULL
) {
1311 attr
= ippNextAttribute(response
);
1315 if (strcmp(ippGetName(attr
), "job-id") == 0 &&
1316 ippGetValueTag(attr
) == IPP_TAG_INTEGER
)
1317 job_id
= ippGetInteger(attr
, 0);
1319 if (strcmp(ippGetName(attr
), "job-k-octets") == 0 &&
1320 ippGetValueTag(attr
) == IPP_TAG_INTEGER
)
1321 job_k_octets
= ippGetInteger(attr
, 0);
1323 if (strcmp(ippGetName(attr
), "job-priority") == 0 &&
1324 ippGetValueTag(attr
) == IPP_TAG_INTEGER
)
1325 job_priority
= ippGetInteger(attr
, 0);
1327 if (strcmp(ippGetName(attr
), "job-state") == 0 &&
1328 ippGetValueTag(attr
) == IPP_TAG_ENUM
)
1329 job_status
= (ipp_jstate_t
)ippGetInteger(attr
, 0);
1331 if (strcmp(ippGetName(attr
), "time-at-creation") == 0 &&
1332 ippGetValueTag(attr
) == IPP_TAG_INTEGER
)
1333 job_time
= ippGetInteger(attr
, 0);
1335 if (strcmp(ippGetName(attr
), "job-name") == 0 &&
1336 ippGetValueTag(attr
) == IPP_TAG_NAME
) {
1337 if (!pull_utf8_talloc(frame
,
1339 ippGetString(attr
, 0, NULL
),
1345 if (strcmp(ippGetName(attr
), "job-originating-user-name") == 0 &&
1346 ippGetValueTag(attr
) == IPP_TAG_NAME
) {
1347 if (!pull_utf8_talloc(frame
,
1349 ippGetString(attr
, 0, NULL
),
1355 attr
= ippNextAttribute(response
);
1359 * See if we have everything needed...
1362 if (user_name
== NULL
|| job_name
== NULL
|| job_id
== 0) {
1369 temp
->sysjob
= job_id
;
1370 temp
->size
= job_k_octets
* 1024;
1371 temp
->status
= job_status
== IPP_JOB_PENDING
? LPQ_QUEUED
:
1372 job_status
== IPP_JOB_STOPPED
? LPQ_PAUSED
:
1373 job_status
== IPP_JOB_HELD
? LPQ_PAUSED
:
1375 temp
->priority
= job_priority
;
1376 temp
->time
= job_time
;
1377 strlcpy(temp
->fs_user
, user_name
, sizeof(temp
->fs_user
));
1378 strlcpy(temp
->fs_file
, job_name
, sizeof(temp
->fs_file
));
1386 ippDelete(response
);
1390 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the
1391 * following attributes:
1393 * attributes-charset
1394 * attributes-natural-language
1395 * requested-attributes
1401 ippSetOperation(request
, IPP_GET_PRINTER_ATTRIBUTES
);
1402 ippSetRequestId(request
, 1);
1404 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1405 "attributes-charset", NULL
, "utf-8");
1407 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1408 "attributes-natural-language", NULL
, language
->language
);
1410 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1411 "requested-attributes",
1412 (sizeof(pattrs
) / sizeof(pattrs
[0])),
1415 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1416 "printer-uri", NULL
, uri
);
1419 * Do the request and get back a response...
1422 if ((response
= cupsDoRequest(http
, request
, "/")) == NULL
) {
1423 DEBUG(0,("Unable to get printer status for %s - %s\n", printername
,
1424 ippErrorString(cupsLastError())));
1428 if (ippGetStatusCode(response
) >= IPP_OK_CONFLICT
) {
1429 DEBUG(0,("Unable to get printer status for %s - %s\n", printername
,
1430 ippErrorString(ippGetStatusCode(response
))));
1435 * Get the current printer status and convert it to the SAMBA values.
1438 if ((attr
= ippFindAttribute(response
, "printer-state", IPP_TAG_ENUM
)) != NULL
) {
1439 if (ippGetInteger(attr
, 0) == IPP_PRINTER_STOPPED
)
1440 status
->status
= LPSTAT_STOPPED
;
1442 status
->status
= LPSTAT_OK
;
1445 if ((attr
= ippFindAttribute(response
, "printer-state-message",
1446 IPP_TAG_TEXT
)) != NULL
) {
1448 if (!pull_utf8_talloc(frame
, &msg
,
1449 ippGetString(attr
, 0, NULL
),
1455 fstrcpy(status
->message
, msg
);
1461 * Return the job queue...
1467 ippDelete(response
);
1470 cupsLangFree(language
);
1481 * 'cups_queue_pause()' - Pause a print queue.
1484 static int cups_queue_pause(int snum
)
1486 TALLOC_CTX
*frame
= talloc_stackframe();
1487 int ret
= 1; /* Return value */
1488 http_t
*http
= NULL
; /* HTTP connection to server */
1489 ipp_t
*request
= NULL
, /* IPP Request */
1490 *response
= NULL
; /* IPP Response */
1491 cups_lang_t
*language
= NULL
; /* Default language */
1492 char *printername
= NULL
;
1493 char *username
= NULL
;
1494 char uri
[HTTP_MAX_URI
] = {0}; /* printer-uri attribute */
1495 http_uri_status_t ustatus
;
1498 DEBUG(5,("cups_queue_pause(%d)\n", snum
));
1501 * Make sure we don't ask for passwords...
1504 cupsSetPasswordCB(cups_passwd_cb
);
1507 * Try to connect to the server...
1510 if ((http
= cups_connect(frame
)) == NULL
) {
1515 * Build an IPP_PAUSE_PRINTER request, which requires the following
1518 * attributes-charset
1519 * attributes-natural-language
1521 * requesting-user-name
1526 ippSetOperation(request
, IPP_PAUSE_PRINTER
);
1527 ippSetRequestId(request
, 1);
1529 language
= cupsLangDefault();
1531 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1532 "attributes-charset", NULL
, "utf-8");
1534 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1535 "attributes-natural-language", NULL
, language
->language
);
1537 if (!push_utf8_talloc(frame
, &printername
,
1538 lp_printername(talloc_tos(), snum
), &size
)) {
1541 ustatus
= httpAssembleURIf(HTTP_URI_CODING_ALL
,
1545 NULL
, /* username */
1550 if (ustatus
!= HTTP_URI_STATUS_OK
) {
1554 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
1556 if (!push_utf8_talloc(frame
, &username
, current_user_info
.unix_name
, &size
)) {
1559 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
1563 * Do the request and get back a response...
1566 if ((response
= cupsDoRequest(http
, request
, "/admin/")) != NULL
) {
1567 if (ippGetStatusCode(response
) >= IPP_OK_CONFLICT
) {
1568 DEBUG(0,("Unable to pause printer %s - %s\n",
1569 lp_printername(talloc_tos(), snum
),
1570 ippErrorString(cupsLastError())));
1575 DEBUG(0,("Unable to pause printer %s - %s\n",
1576 lp_printername(talloc_tos(), snum
),
1577 ippErrorString(cupsLastError())));
1582 ippDelete(response
);
1585 cupsLangFree(language
);
1596 * 'cups_queue_resume()' - Restart a print queue.
1599 static int cups_queue_resume(int snum
)
1601 TALLOC_CTX
*frame
= talloc_stackframe();
1602 int ret
= 1; /* Return value */
1603 http_t
*http
= NULL
; /* HTTP connection to server */
1604 ipp_t
*request
= NULL
, /* IPP Request */
1605 *response
= NULL
; /* IPP Response */
1606 cups_lang_t
*language
= NULL
; /* Default language */
1607 char *printername
= NULL
;
1608 char *username
= NULL
;
1609 char uri
[HTTP_MAX_URI
] = {0}; /* printer-uri attribute */
1610 http_uri_status_t ustatus
;
1613 DEBUG(5,("cups_queue_resume(%d)\n", snum
));
1616 * Make sure we don't ask for passwords...
1619 cupsSetPasswordCB(cups_passwd_cb
);
1622 * Try to connect to the server...
1625 if ((http
= cups_connect(frame
)) == NULL
) {
1630 * Build an IPP_RESUME_PRINTER request, which requires the following
1633 * attributes-charset
1634 * attributes-natural-language
1636 * requesting-user-name
1641 ippSetOperation(request
, IPP_RESUME_PRINTER
);
1642 ippSetRequestId(request
, 1);
1644 language
= cupsLangDefault();
1646 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1647 "attributes-charset", NULL
, "utf-8");
1649 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1650 "attributes-natural-language", NULL
, language
->language
);
1652 if (!push_utf8_talloc(frame
, &printername
, lp_printername(talloc_tos(), snum
),
1656 ustatus
= httpAssembleURIf(HTTP_URI_CODING_ALL
,
1660 NULL
, /* username */
1665 if (ustatus
!= HTTP_URI_STATUS_OK
) {
1669 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
1671 if (!push_utf8_talloc(frame
, &username
, current_user_info
.unix_name
, &size
)) {
1674 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
1678 * Do the request and get back a response...
1681 if ((response
= cupsDoRequest(http
, request
, "/admin/")) != NULL
) {
1682 if (ippGetStatusCode(response
) >= IPP_OK_CONFLICT
) {
1683 DEBUG(0,("Unable to resume printer %s - %s\n",
1684 lp_printername(talloc_tos(), snum
),
1685 ippErrorString(cupsLastError())));
1690 DEBUG(0,("Unable to resume printer %s - %s\n",
1691 lp_printername(talloc_tos(), snum
),
1692 ippErrorString(cupsLastError())));
1697 ippDelete(response
);
1700 cupsLangFree(language
);
1709 /*******************************************************************
1710 * CUPS printing interface definitions...
1711 ******************************************************************/
1713 struct printif cups_printif
=
1726 /* this keeps fussy compilers happy */
1727 void print_cups_dummy(void);
1728 void print_cups_dummy(void) {}
1729 #endif /* HAVE_CUPS */