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>
35 #if (CUPS_VERSION_MAJOR > 1) || (CUPS_VERSION_MINOR > 5)
36 #define HAVE_CUPS_1_6 1
40 #define ippGetGroupTag(attr) attr->group_tag
41 #define ippGetName(attr) attr->name
42 #define ippGetValueTag(attr) attr->value_tag
43 #define ippGetStatusCode(ipp) ipp->request.status.status_code
44 #define ippGetInteger(attr, element) attr->values[element].integer
45 #define ippGetString(attr, element, language) attr->values[element].string.text
47 static ipp_attribute_t
*
48 ippFirstAttribute(ipp_t
*ipp
)
52 return (ipp
->current
= ipp
->attrs
);
55 static ipp_attribute_t
*
56 ippNextAttribute(ipp_t
*ipp
)
58 if (!ipp
|| !ipp
->current
)
60 return (ipp
->current
= ipp
->current
->next
);
63 static int ippSetOperation(ipp_t
*ipp
, ipp_op_t op
)
65 ipp
->request
.op
.operation_id
= op
;
69 static int ippSetRequestId(ipp_t
*ipp
, int request_id
)
71 ipp
->request
.any
.request_id
= request_id
;
76 static SIG_ATOMIC_T gotalarm
;
78 /***************************************************************
79 Signal function to tell us we timed out.
80 ****************************************************************/
82 static void gotalarm_sig(int signum
)
87 extern userdom_struct current_user_info
;
90 * 'cups_passwd_cb()' - The CUPS password callback...
93 static const char * /* O - Password or NULL */
94 cups_passwd_cb(const char *prompt
) /* I - Prompt */
97 * Always return NULL to indicate that no password is available...
103 static http_t
*cups_connect(TALLOC_CTX
*frame
)
106 char *server
= NULL
, *p
= NULL
;
108 int timeout
= lp_cups_connection_timeout();
111 if (lp_cups_server(talloc_tos()) != NULL
&& strlen(lp_cups_server(talloc_tos())) > 0) {
112 if (!push_utf8_talloc(frame
, &server
, lp_cups_server(talloc_tos()), &size
)) {
116 server
= talloc_strdup(frame
,cupsServer());
122 p
= strchr(server
, ':');
130 DEBUG(10, ("connecting to cups server %s:%d\n",
136 CatchSignal(SIGALRM
, gotalarm_sig
);
140 #ifdef HAVE_HTTPCONNECTENCRYPT
141 http
= httpConnectEncrypt(server
, port
, lp_cups_encrypt());
143 http
= httpConnect(server
, port
);
147 CatchSignal(SIGALRM
, SIG_IGN
);
151 DEBUG(3,("Unable to connect to CUPS server %s:%d - %s\n",
152 server
, port
, strerror(errno
)));
158 static bool send_pcap_blob(DATA_BLOB
*pcap_blob
, int fd
)
162 ret
= sys_write(fd
, &pcap_blob
->length
, sizeof(pcap_blob
->length
));
163 if (ret
!= sizeof(pcap_blob
->length
)) {
167 ret
= sys_write(fd
, pcap_blob
->data
, pcap_blob
->length
);
168 if (ret
!= pcap_blob
->length
) {
172 DEBUG(10, ("successfully sent blob of len %d\n", (int)ret
));
176 static bool recv_pcap_blob(TALLOC_CTX
*mem_ctx
, int fd
, DATA_BLOB
*pcap_blob
)
181 ret
= sys_read(fd
, &blob_len
, sizeof(blob_len
));
182 if (ret
!= sizeof(blob_len
)) {
186 *pcap_blob
= data_blob_talloc_named(mem_ctx
, NULL
, blob_len
,
188 if (pcap_blob
->length
!= blob_len
) {
191 ret
= sys_read(fd
, pcap_blob
->data
, blob_len
);
192 if (ret
!= blob_len
) {
193 talloc_free(pcap_blob
->data
);
197 DEBUG(10, ("successfully recvd blob of len %d\n", (int)ret
));
201 static bool process_cups_printers_response(TALLOC_CTX
*mem_ctx
,
203 struct pcap_data
*pcap_data
)
205 ipp_attribute_t
*attr
;
208 char *location
= NULL
;
209 struct pcap_printer
*printer
;
212 for (attr
= ippFirstAttribute(response
); attr
!= NULL
;) {
214 * Skip leading attributes until we hit a printer...
217 while (attr
!= NULL
&& ippGetGroupTag(attr
) != IPP_TAG_PRINTER
)
218 attr
= ippNextAttribute(response
);
224 * Pull the needed attributes from this printer...
230 while (attr
!= NULL
&& ippGetGroupTag(attr
) == IPP_TAG_PRINTER
) {
232 if (strcmp(ippGetName(attr
), "printer-name") == 0 &&
233 ippGetValueTag(attr
) == IPP_TAG_NAME
) {
234 if (!pull_utf8_talloc(mem_ctx
,
236 ippGetString(attr
, 0, NULL
),
242 if (strcmp(ippGetName(attr
), "printer-info") == 0 &&
243 ippGetValueTag(attr
) == IPP_TAG_TEXT
) {
244 if (!pull_utf8_talloc(mem_ctx
,
246 ippGetString(attr
, 0, NULL
),
252 if (strcmp(ippGetName(attr
), "printer-location") == 0 &&
253 ippGetValueTag(attr
) == IPP_TAG_TEXT
) {
254 if (!pull_utf8_talloc(mem_ctx
,
256 ippGetString(attr
, 0, NULL
),
262 attr
= ippNextAttribute(response
);
266 * See if we have everything needed...
272 if (pcap_data
->count
== 0) {
273 printer
= talloc_array(mem_ctx
, struct pcap_printer
, 1);
275 printer
= talloc_realloc(mem_ctx
, pcap_data
->printers
,
277 pcap_data
->count
+ 1);
279 if (printer
== NULL
) {
282 pcap_data
->printers
= printer
;
283 pcap_data
->printers
[pcap_data
->count
].name
= name
;
284 pcap_data
->printers
[pcap_data
->count
].info
= info
;
285 pcap_data
->printers
[pcap_data
->count
].location
= location
;
295 * request printer list from cups, send result back to up parent via fd.
296 * returns true if the (possibly failed) result was successfuly sent to parent.
298 static bool cups_cache_reload_async(int fd
)
300 TALLOC_CTX
*frame
= talloc_stackframe();
301 struct pcap_data pcap_data
;
302 http_t
*http
= NULL
; /* HTTP connection to server */
303 ipp_t
*request
= NULL
, /* IPP Request */
304 *response
= NULL
; /* IPP Response */
305 cups_lang_t
*language
= NULL
; /* Default language */
306 static const char *requested
[] =/* Requested attributes */
313 enum ndr_err_code ndr_ret
;
316 ZERO_STRUCT(pcap_data
);
317 pcap_data
.status
= NT_STATUS_UNSUCCESSFUL
;
319 DEBUG(5, ("reloading cups printcap cache\n"));
322 * Make sure we don't ask for passwords...
325 cupsSetPasswordCB(cups_passwd_cb
);
327 if ((http
= cups_connect(frame
)) == NULL
) {
332 * Build a CUPS_GET_PRINTERS request, which requires the following
336 * attributes-natural-language
337 * requested-attributes
342 ippSetOperation(request
, CUPS_GET_PRINTERS
);
343 ippSetRequestId(request
, 1);
345 language
= cupsLangDefault();
347 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
348 "attributes-charset", NULL
, "utf-8");
350 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
351 "attributes-natural-language", NULL
, language
->language
);
353 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
354 "requested-attributes",
355 (sizeof(requested
) / sizeof(requested
[0])),
358 if ((response
= cupsDoRequest(http
, request
, "/")) == NULL
) {
359 DEBUG(0,("Unable to get printer list - %s\n",
360 ippErrorString(cupsLastError())));
364 ret
= process_cups_printers_response(frame
, response
, &pcap_data
);
366 DEBUG(0,("failed to process cups response\n"));
374 * Build a CUPS_GET_CLASSES request, which requires the following
378 * attributes-natural-language
379 * requested-attributes
384 ippSetOperation(request
, CUPS_GET_CLASSES
);
385 ippSetRequestId(request
, 1);
387 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
388 "attributes-charset", NULL
, "utf-8");
390 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
391 "attributes-natural-language", NULL
, language
->language
);
393 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
394 "requested-attributes",
395 (sizeof(requested
) / sizeof(requested
[0])),
398 if ((response
= cupsDoRequest(http
, request
, "/")) == NULL
) {
399 DEBUG(0,("Unable to get printer list - %s\n",
400 ippErrorString(cupsLastError())));
404 ret
= process_cups_printers_response(frame
, response
, &pcap_data
);
406 DEBUG(0,("failed to process cups response\n"));
410 pcap_data
.status
= NT_STATUS_OK
;
416 cupsLangFree(language
);
422 ndr_ret
= ndr_push_struct_blob(&pcap_blob
, frame
, &pcap_data
,
423 (ndr_push_flags_fn_t
)ndr_push_pcap_data
);
424 if (ndr_ret
== NDR_ERR_SUCCESS
) {
425 ret
= send_pcap_blob(&pcap_blob
, fd
);
432 static struct tevent_fd
*cache_fd_event
;
434 static bool cups_pcap_load_async(struct tevent_context
*ev
,
435 struct messaging_context
*msg_ctx
,
444 if (cache_fd_event
) {
445 DEBUG(3,("cups_pcap_load_async: already waiting for "
446 "a refresh event\n" ));
450 DEBUG(5,("cups_pcap_load_async: asynchronously loading cups printers\n"));
452 if (pipe(fds
) == -1) {
457 if (pid
== (pid_t
)-1) {
458 DEBUG(10,("cups_pcap_load_async: fork failed %s\n",
466 DEBUG(10,("cups_pcap_load_async: child pid = %u\n",
467 (unsigned int)pid
));
476 close_all_print_db();
478 status
= reinit_after_fork(msg_ctx
, ev
, true, NULL
);
479 if (!NT_STATUS_IS_OK(status
)) {
480 DEBUG(0,("cups_pcap_load_async: reinit_after_fork() failed\n"));
481 smb_panic("cups_pcap_load_async: reinit_after_fork() failed");
485 cups_cache_reload_async(fds
[1]);
487 TALLOC_FREE(msg_ctx
);
491 struct cups_async_cb_args
{
493 struct tevent_context
*event_ctx
;
494 struct messaging_context
*msg_ctx
;
495 void (*post_cache_fill_fn
)(struct tevent_context
*,
496 struct messaging_context
*);
499 static void cups_async_callback(struct tevent_context
*event_ctx
,
500 struct tevent_fd
*event
,
504 TALLOC_CTX
*frame
= talloc_stackframe();
505 struct cups_async_cb_args
*cb_args
= (struct cups_async_cb_args
*)p
;
506 struct pcap_cache
*tmp_pcap_cache
= NULL
;
508 struct pcap_data pcap_data
;
510 enum ndr_err_code ndr_ret
;
513 DEBUG(5,("cups_async_callback: callback received for printer data. "
514 "fd = %d\n", cb_args
->pipe_fd
));
516 ret_ok
= recv_pcap_blob(frame
, cb_args
->pipe_fd
, &pcap_blob
);
518 DEBUG(0,("failed to recv pcap blob\n"));
522 ndr_ret
= ndr_pull_struct_blob(&pcap_blob
, frame
, &pcap_data
,
523 (ndr_pull_flags_fn_t
)ndr_pull_pcap_data
);
524 if (ndr_ret
!= NDR_ERR_SUCCESS
) {
528 if (!NT_STATUS_IS_OK(pcap_data
.status
)) {
529 DEBUG(3,("failed to retrieve printer list: %s\n",
530 nt_errstr(pcap_data
.status
)));
534 for (i
= 0; i
< pcap_data
.count
; i
++) {
535 ret_ok
= pcap_cache_add_specific(&tmp_pcap_cache
,
536 pcap_data
.printers
[i
].name
,
537 pcap_data
.printers
[i
].info
,
538 pcap_data
.printers
[i
].location
);
540 DEBUG(0, ("failed to add to tmp pcap cache\n"));
545 /* replace the system-wide pcap cache with a (possibly empty) new one */
546 ret_ok
= pcap_cache_replace(tmp_pcap_cache
);
548 DEBUG(0, ("failed to replace pcap cache\n"));
549 } else if (cb_args
->post_cache_fill_fn
!= NULL
) {
550 /* Caller requested post cache fill callback */
551 cb_args
->post_cache_fill_fn(cb_args
->event_ctx
,
555 pcap_cache_destroy_specific(&tmp_pcap_cache
);
557 close(cb_args
->pipe_fd
);
558 TALLOC_FREE(cb_args
);
559 TALLOC_FREE(cache_fd_event
);
562 bool cups_cache_reload(struct tevent_context
*ev
,
563 struct messaging_context
*msg_ctx
,
564 void (*post_cache_fill_fn
)(struct tevent_context
*,
565 struct messaging_context
*))
567 struct cups_async_cb_args
*cb_args
;
570 cb_args
= talloc(NULL
, struct cups_async_cb_args
);
571 if (cb_args
== NULL
) {
575 cb_args
->post_cache_fill_fn
= post_cache_fill_fn
;
576 cb_args
->event_ctx
= ev
;
577 cb_args
->msg_ctx
= msg_ctx
;
578 p_pipe_fd
= &cb_args
->pipe_fd
;
581 /* Set up an async refresh. */
582 if (!cups_pcap_load_async(ev
, msg_ctx
, p_pipe_fd
)) {
583 talloc_free(cb_args
);
587 DEBUG(10,("cups_cache_reload: async read on fd %d\n",
590 /* Trigger an event when the pipe can be read. */
591 cache_fd_event
= tevent_add_fd(ev
,
596 if (!cache_fd_event
) {
598 TALLOC_FREE(cb_args
);
606 * 'cups_job_delete()' - Delete a job.
609 static int cups_job_delete(const char *sharename
, const char *lprm_command
, struct printjob
*pjob
)
611 TALLOC_CTX
*frame
= talloc_stackframe();
612 int ret
= 1; /* Return value */
613 http_t
*http
= NULL
; /* HTTP connection to server */
614 ipp_t
*request
= NULL
, /* IPP Request */
615 *response
= NULL
; /* IPP Response */
616 cups_lang_t
*language
= NULL
; /* Default language */
618 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
621 DEBUG(5,("cups_job_delete(%s, %p (%d))\n", sharename
, pjob
, pjob
->sysjob
));
624 * Make sure we don't ask for passwords...
627 cupsSetPasswordCB(cups_passwd_cb
);
630 * Try to connect to the server...
633 if ((http
= cups_connect(frame
)) == NULL
) {
638 * Build an IPP_CANCEL_JOB request, which requires the following
642 * attributes-natural-language
644 * requesting-user-name
649 ippSetOperation(request
, IPP_CANCEL_JOB
);
650 ippSetRequestId(request
, 1);
652 language
= cupsLangDefault();
654 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
655 "attributes-charset", NULL
, "utf-8");
657 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
658 "attributes-natural-language", NULL
, language
->language
);
660 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/jobs/%d", pjob
->sysjob
);
662 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
664 if (!push_utf8_talloc(frame
, &user
, pjob
->user
, &size
)) {
668 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
672 * Do the request and get back a response...
675 if ((response
= cupsDoRequest(http
, request
, "/jobs")) != NULL
) {
676 if (ippGetStatusCode(response
) >= IPP_OK_CONFLICT
) {
677 DEBUG(0,("Unable to cancel job %d - %s\n", pjob
->sysjob
,
678 ippErrorString(cupsLastError())));
683 DEBUG(0,("Unable to cancel job %d - %s\n", pjob
->sysjob
,
684 ippErrorString(cupsLastError())));
692 cupsLangFree(language
);
703 * 'cups_job_pause()' - Pause a job.
706 static int cups_job_pause(int snum
, struct printjob
*pjob
)
708 TALLOC_CTX
*frame
= talloc_stackframe();
709 int ret
= 1; /* Return value */
710 http_t
*http
= NULL
; /* HTTP connection to server */
711 ipp_t
*request
= NULL
, /* IPP Request */
712 *response
= NULL
; /* IPP Response */
713 cups_lang_t
*language
= NULL
; /* Default language */
715 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
718 DEBUG(5,("cups_job_pause(%d, %p (%d))\n", snum
, pjob
, pjob
->sysjob
));
721 * Make sure we don't ask for passwords...
724 cupsSetPasswordCB(cups_passwd_cb
);
727 * Try to connect to the server...
730 if ((http
= cups_connect(frame
)) == NULL
) {
735 * Build an IPP_HOLD_JOB request, which requires the following
739 * attributes-natural-language
741 * requesting-user-name
746 ippSetOperation(request
, IPP_HOLD_JOB
);
747 ippSetRequestId(request
, 1);
749 language
= cupsLangDefault();
751 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
752 "attributes-charset", NULL
, "utf-8");
754 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
755 "attributes-natural-language", NULL
, language
->language
);
757 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/jobs/%d", pjob
->sysjob
);
759 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
761 if (!push_utf8_talloc(frame
, &user
, pjob
->user
, &size
)) {
764 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
768 * Do the request and get back a response...
771 if ((response
= cupsDoRequest(http
, request
, "/jobs")) != NULL
) {
772 if (ippGetStatusCode(response
) >= IPP_OK_CONFLICT
) {
773 DEBUG(0,("Unable to hold job %d - %s\n", pjob
->sysjob
,
774 ippErrorString(cupsLastError())));
779 DEBUG(0,("Unable to hold job %d - %s\n", pjob
->sysjob
,
780 ippErrorString(cupsLastError())));
788 cupsLangFree(language
);
799 * 'cups_job_resume()' - Resume a paused job.
802 static int cups_job_resume(int snum
, struct printjob
*pjob
)
804 TALLOC_CTX
*frame
= talloc_stackframe();
805 int ret
= 1; /* Return value */
806 http_t
*http
= NULL
; /* HTTP connection to server */
807 ipp_t
*request
= NULL
, /* IPP Request */
808 *response
= NULL
; /* IPP Response */
809 cups_lang_t
*language
= NULL
; /* Default language */
811 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
814 DEBUG(5,("cups_job_resume(%d, %p (%d))\n", snum
, pjob
, pjob
->sysjob
));
817 * Make sure we don't ask for passwords...
820 cupsSetPasswordCB(cups_passwd_cb
);
823 * Try to connect to the server...
826 if ((http
= cups_connect(frame
)) == NULL
) {
831 * Build an IPP_RELEASE_JOB request, which requires the following
835 * attributes-natural-language
837 * requesting-user-name
842 ippSetOperation(request
, IPP_RELEASE_JOB
);
843 ippSetRequestId(request
, 1);
845 language
= cupsLangDefault();
847 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
848 "attributes-charset", NULL
, "utf-8");
850 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
851 "attributes-natural-language", NULL
, language
->language
);
853 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/jobs/%d", pjob
->sysjob
);
855 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
857 if (!push_utf8_talloc(frame
, &user
, pjob
->user
, &size
)) {
860 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
864 * Do the request and get back a response...
867 if ((response
= cupsDoRequest(http
, request
, "/jobs")) != NULL
) {
868 if (ippGetStatusCode(response
) >= IPP_OK_CONFLICT
) {
869 DEBUG(0,("Unable to release job %d - %s\n", pjob
->sysjob
,
870 ippErrorString(cupsLastError())));
875 DEBUG(0,("Unable to release job %d - %s\n", pjob
->sysjob
,
876 ippErrorString(cupsLastError())));
884 cupsLangFree(language
);
895 * 'cups_job_submit()' - Submit a job for printing.
898 static int cups_job_submit(int snum
, struct printjob
*pjob
,
899 enum printing_types printing_type
,
902 TALLOC_CTX
*frame
= talloc_stackframe();
903 int ret
= 1; /* Return value */
904 http_t
*http
= NULL
; /* HTTP connection to server */
905 ipp_t
*request
= NULL
, /* IPP Request */
906 *response
= NULL
; /* IPP Response */
907 ipp_attribute_t
*attr_job_id
= NULL
; /* IPP Attribute "job-id" */
908 cups_lang_t
*language
= NULL
; /* Default language */
909 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
910 char *new_jobname
= NULL
;
912 cups_option_t
*options
= NULL
;
913 char *printername
= NULL
;
915 char *jobname
= NULL
;
916 char *cupsoptions
= NULL
;
917 char *filename
= NULL
;
920 DEBUG(5,("cups_job_submit(%d, %p)\n", snum
, pjob
));
923 * Make sure we don't ask for passwords...
926 cupsSetPasswordCB(cups_passwd_cb
);
929 * Try to connect to the server...
932 if ((http
= cups_connect(frame
)) == NULL
) {
937 * Build an IPP_PRINT_JOB request, which requires the following
941 * attributes-natural-language
943 * requesting-user-name
949 ippSetOperation(request
, IPP_PRINT_JOB
);
950 ippSetRequestId(request
, 1);
952 language
= cupsLangDefault();
954 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
955 "attributes-charset", NULL
, "utf-8");
957 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
958 "attributes-natural-language", NULL
, language
->language
);
960 if (!push_utf8_talloc(frame
, &printername
,
961 lp_printername(talloc_tos(), snum
),
965 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/printers/%s",
968 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
969 "printer-uri", NULL
, uri
);
971 if (!push_utf8_talloc(frame
, &user
, pjob
->user
, &size
)) {
974 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
977 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
978 "job-originating-host-name", NULL
,
979 pjob
->clientmachine
);
981 if (!push_utf8_talloc(frame
, &jobname
, pjob
->jobname
, &size
)) {
984 new_jobname
= talloc_asprintf(frame
,
985 "%s%.8u %s", PRINT_SPOOL_PREFIX
,
986 pjob
->jobid
, jobname
);
987 if (new_jobname
== NULL
) {
991 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name", NULL
,
995 * add any options defined in smb.conf
998 if (!push_utf8_talloc(frame
, &cupsoptions
,
999 lp_cups_options(talloc_tos(), snum
), &size
)) {
1004 num_options
= cupsParseOptions(cupsoptions
, num_options
, &options
);
1007 cupsEncodeOptions(request
, num_options
, options
);
1010 * Do the request and get back a response...
1013 slprintf(uri
, sizeof(uri
) - 1, "/printers/%s", printername
);
1015 if (!push_utf8_talloc(frame
, &filename
, pjob
->filename
, &size
)) {
1018 if ((response
= cupsDoFileRequest(http
, request
, uri
, pjob
->filename
)) != NULL
) {
1019 if (ippGetStatusCode(response
) >= IPP_OK_CONFLICT
) {
1020 DEBUG(0,("Unable to print file to %s - %s\n",
1021 lp_printername(talloc_tos(), snum
),
1022 ippErrorString(cupsLastError())));
1025 attr_job_id
= ippFindAttribute(response
, "job-id", IPP_TAG_INTEGER
);
1027 pjob
->sysjob
= ippGetInteger(attr_job_id
, 0);
1028 DEBUG(5,("cups_job_submit: job-id %d\n", pjob
->sysjob
));
1030 DEBUG(0,("Missing job-id attribute in IPP response"));
1034 DEBUG(0,("Unable to print file to `%s' - %s\n",
1035 lp_printername(talloc_tos(), snum
),
1036 ippErrorString(cupsLastError())));
1040 unlink(pjob
->filename
);
1041 /* else print_job_end will do it for us */
1045 ippDelete(response
);
1048 cupsLangFree(language
);
1059 * 'cups_queue_get()' - Get all the jobs in the print queue.
1062 static int cups_queue_get(const char *sharename
,
1063 enum printing_types printing_type
,
1065 print_queue_struct
**q
,
1066 print_status_struct
*status
)
1068 TALLOC_CTX
*frame
= talloc_stackframe();
1069 char *printername
= NULL
;
1070 http_t
*http
= NULL
; /* HTTP connection to server */
1071 ipp_t
*request
= NULL
, /* IPP Request */
1072 *response
= NULL
; /* IPP Response */
1073 ipp_attribute_t
*attr
= NULL
; /* Current attribute */
1074 cups_lang_t
*language
= NULL
; /* Default language */
1075 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
1076 int qcount
= 0, /* Number of active queue entries */
1077 qalloc
= 0; /* Number of queue entries allocated */
1078 print_queue_struct
*queue
= NULL
, /* Queue entries */
1079 *temp
; /* Temporary pointer for queue */
1080 char *user_name
= NULL
, /* job-originating-user-name attribute */
1081 *job_name
= NULL
; /* job-name attribute */
1082 int job_id
; /* job-id attribute */
1083 int job_k_octets
; /* job-k-octets attribute */
1084 time_t job_time
; /* time-at-creation attribute */
1085 ipp_jstate_t job_status
; /* job-status attribute */
1086 int job_priority
; /* job-priority attribute */
1088 static const char *jattrs
[] = /* Requested job attributes */
1093 "job-originating-user-name",
1098 static const char *pattrs
[] = /* Requested printer attributes */
1101 "printer-state-message"
1106 /* HACK ALERT!!! The problem with support the 'printer name'
1107 option is that we key the tdb off the sharename. So we will
1108 overload the lpq_command string to pass in the printername
1109 (which is basically what we do for non-cups printers ... using
1110 the lpq_command to get the queue listing). */
1112 if (!push_utf8_talloc(frame
, &printername
, lpq_command
, &size
)) {
1115 DEBUG(5,("cups_queue_get(%s, %p, %p)\n", lpq_command
, q
, status
));
1118 * Make sure we don't ask for passwords...
1121 cupsSetPasswordCB(cups_passwd_cb
);
1124 * Try to connect to the server...
1127 if ((http
= cups_connect(frame
)) == NULL
) {
1132 * Generate the printer URI...
1135 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/printers/%s", printername
);
1138 * Build an IPP_GET_JOBS request, which requires the following
1141 * attributes-charset
1142 * attributes-natural-language
1143 * requested-attributes
1149 ippSetOperation(request
, IPP_GET_JOBS
);
1150 ippSetRequestId(request
, 1);
1152 language
= cupsLangDefault();
1154 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1155 "attributes-charset", NULL
, "utf-8");
1157 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1158 "attributes-natural-language", NULL
, language
->language
);
1160 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1161 "requested-attributes",
1162 (sizeof(jattrs
) / sizeof(jattrs
[0])),
1165 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1166 "printer-uri", NULL
, uri
);
1169 * Do the request and get back a response...
1172 if ((response
= cupsDoRequest(http
, request
, "/")) == NULL
) {
1173 DEBUG(0,("Unable to get jobs for %s - %s\n", uri
,
1174 ippErrorString(cupsLastError())));
1178 if (ippGetStatusCode(response
) >= IPP_OK_CONFLICT
) {
1179 DEBUG(0,("Unable to get jobs for %s - %s\n", uri
,
1180 ippErrorString(ippGetStatusCode(response
))));
1185 * Process the jobs...
1192 for (attr
= ippFirstAttribute(response
); attr
!= NULL
; attr
= ippNextAttribute(response
)) {
1194 * Skip leading attributes until we hit a job...
1197 while (attr
!= NULL
&& ippGetGroupTag(attr
) != IPP_TAG_JOB
)
1198 attr
= ippNextAttribute(response
);
1204 * Allocate memory as needed...
1206 if (qcount
>= qalloc
) {
1209 queue
= SMB_REALLOC_ARRAY(queue
, print_queue_struct
, qalloc
);
1211 if (queue
== NULL
) {
1212 DEBUG(0,("cups_queue_get: Not enough memory!"));
1218 temp
= queue
+ qcount
;
1219 memset(temp
, 0, sizeof(print_queue_struct
));
1222 * Pull the needed attributes from this job...
1227 job_status
= IPP_JOB_PENDING
;
1233 while (attr
!= NULL
&& ippGetGroupTag(attr
) == IPP_TAG_JOB
) {
1234 if (ippGetName(attr
) == NULL
) {
1235 attr
= ippNextAttribute(response
);
1239 if (strcmp(ippGetName(attr
), "job-id") == 0 &&
1240 ippGetValueTag(attr
) == IPP_TAG_INTEGER
)
1241 job_id
= ippGetInteger(attr
, 0);
1243 if (strcmp(ippGetName(attr
), "job-k-octets") == 0 &&
1244 ippGetValueTag(attr
) == IPP_TAG_INTEGER
)
1245 job_k_octets
= ippGetInteger(attr
, 0);
1247 if (strcmp(ippGetName(attr
), "job-priority") == 0 &&
1248 ippGetValueTag(attr
) == IPP_TAG_INTEGER
)
1249 job_priority
= ippGetInteger(attr
, 0);
1251 if (strcmp(ippGetName(attr
), "job-state") == 0 &&
1252 ippGetValueTag(attr
) == IPP_TAG_ENUM
)
1253 job_status
= (ipp_jstate_t
)ippGetInteger(attr
, 0);
1255 if (strcmp(ippGetName(attr
), "time-at-creation") == 0 &&
1256 ippGetValueTag(attr
) == IPP_TAG_INTEGER
)
1257 job_time
= ippGetInteger(attr
, 0);
1259 if (strcmp(ippGetName(attr
), "job-name") == 0 &&
1260 ippGetValueTag(attr
) == IPP_TAG_NAME
) {
1261 if (!pull_utf8_talloc(frame
,
1263 ippGetString(attr
, 0, NULL
),
1269 if (strcmp(ippGetName(attr
), "job-originating-user-name") == 0 &&
1270 ippGetValueTag(attr
) == IPP_TAG_NAME
) {
1271 if (!pull_utf8_talloc(frame
,
1273 ippGetString(attr
, 0, NULL
),
1279 attr
= ippNextAttribute(response
);
1283 * See if we have everything needed...
1286 if (user_name
== NULL
|| job_name
== NULL
|| job_id
== 0) {
1293 temp
->sysjob
= job_id
;
1294 temp
->size
= job_k_octets
* 1024;
1295 temp
->status
= job_status
== IPP_JOB_PENDING
? LPQ_QUEUED
:
1296 job_status
== IPP_JOB_STOPPED
? LPQ_PAUSED
:
1297 job_status
== IPP_JOB_HELD
? LPQ_PAUSED
:
1299 temp
->priority
= job_priority
;
1300 temp
->time
= job_time
;
1301 strlcpy(temp
->fs_user
, user_name
, sizeof(temp
->fs_user
));
1302 strlcpy(temp
->fs_file
, job_name
, sizeof(temp
->fs_file
));
1310 ippDelete(response
);
1314 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the
1315 * following attributes:
1317 * attributes-charset
1318 * attributes-natural-language
1319 * requested-attributes
1325 ippSetOperation(request
, IPP_GET_PRINTER_ATTRIBUTES
);
1326 ippSetRequestId(request
, 1);
1328 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1329 "attributes-charset", NULL
, "utf-8");
1331 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1332 "attributes-natural-language", NULL
, language
->language
);
1334 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1335 "requested-attributes",
1336 (sizeof(pattrs
) / sizeof(pattrs
[0])),
1339 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1340 "printer-uri", NULL
, uri
);
1343 * Do the request and get back a response...
1346 if ((response
= cupsDoRequest(http
, request
, "/")) == NULL
) {
1347 DEBUG(0,("Unable to get printer status for %s - %s\n", printername
,
1348 ippErrorString(cupsLastError())));
1352 if (ippGetStatusCode(response
) >= IPP_OK_CONFLICT
) {
1353 DEBUG(0,("Unable to get printer status for %s - %s\n", printername
,
1354 ippErrorString(ippGetStatusCode(response
))));
1359 * Get the current printer status and convert it to the SAMBA values.
1362 if ((attr
= ippFindAttribute(response
, "printer-state", IPP_TAG_ENUM
)) != NULL
) {
1363 if (ippGetInteger(attr
, 0) == IPP_PRINTER_STOPPED
)
1364 status
->status
= LPSTAT_STOPPED
;
1366 status
->status
= LPSTAT_OK
;
1369 if ((attr
= ippFindAttribute(response
, "printer-state-message",
1370 IPP_TAG_TEXT
)) != NULL
) {
1372 if (!pull_utf8_talloc(frame
, &msg
,
1373 ippGetString(attr
, 0, NULL
),
1379 fstrcpy(status
->message
, msg
);
1385 * Return the job queue...
1391 ippDelete(response
);
1394 cupsLangFree(language
);
1405 * 'cups_queue_pause()' - Pause a print queue.
1408 static int cups_queue_pause(int snum
)
1410 TALLOC_CTX
*frame
= talloc_stackframe();
1411 int ret
= 1; /* Return value */
1412 http_t
*http
= NULL
; /* HTTP connection to server */
1413 ipp_t
*request
= NULL
, /* IPP Request */
1414 *response
= NULL
; /* IPP Response */
1415 cups_lang_t
*language
= NULL
; /* Default language */
1416 char *printername
= NULL
;
1417 char *username
= NULL
;
1418 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
1421 DEBUG(5,("cups_queue_pause(%d)\n", snum
));
1424 * Make sure we don't ask for passwords...
1427 cupsSetPasswordCB(cups_passwd_cb
);
1430 * Try to connect to the server...
1433 if ((http
= cups_connect(frame
)) == NULL
) {
1438 * Build an IPP_PAUSE_PRINTER request, which requires the following
1441 * attributes-charset
1442 * attributes-natural-language
1444 * requesting-user-name
1449 ippSetOperation(request
, IPP_PAUSE_PRINTER
);
1450 ippSetRequestId(request
, 1);
1452 language
= cupsLangDefault();
1454 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1455 "attributes-charset", NULL
, "utf-8");
1457 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1458 "attributes-natural-language", NULL
, language
->language
);
1460 if (!push_utf8_talloc(frame
, &printername
,
1461 lp_printername(talloc_tos(), snum
), &size
)) {
1464 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/printers/%s",
1467 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
1469 if (!push_utf8_talloc(frame
, &username
, current_user_info
.unix_name
, &size
)) {
1472 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
1476 * Do the request and get back a response...
1479 if ((response
= cupsDoRequest(http
, request
, "/admin/")) != NULL
) {
1480 if (ippGetStatusCode(response
) >= IPP_OK_CONFLICT
) {
1481 DEBUG(0,("Unable to pause printer %s - %s\n",
1482 lp_printername(talloc_tos(), snum
),
1483 ippErrorString(cupsLastError())));
1488 DEBUG(0,("Unable to pause printer %s - %s\n",
1489 lp_printername(talloc_tos(), snum
),
1490 ippErrorString(cupsLastError())));
1495 ippDelete(response
);
1498 cupsLangFree(language
);
1509 * 'cups_queue_resume()' - Restart a print queue.
1512 static int cups_queue_resume(int snum
)
1514 TALLOC_CTX
*frame
= talloc_stackframe();
1515 int ret
= 1; /* Return value */
1516 http_t
*http
= NULL
; /* HTTP connection to server */
1517 ipp_t
*request
= NULL
, /* IPP Request */
1518 *response
= NULL
; /* IPP Response */
1519 cups_lang_t
*language
= NULL
; /* Default language */
1520 char *printername
= NULL
;
1521 char *username
= NULL
;
1522 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
1525 DEBUG(5,("cups_queue_resume(%d)\n", snum
));
1528 * Make sure we don't ask for passwords...
1531 cupsSetPasswordCB(cups_passwd_cb
);
1534 * Try to connect to the server...
1537 if ((http
= cups_connect(frame
)) == NULL
) {
1542 * Build an IPP_RESUME_PRINTER request, which requires the following
1545 * attributes-charset
1546 * attributes-natural-language
1548 * requesting-user-name
1553 ippSetOperation(request
, IPP_RESUME_PRINTER
);
1554 ippSetRequestId(request
, 1);
1556 language
= cupsLangDefault();
1558 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1559 "attributes-charset", NULL
, "utf-8");
1561 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1562 "attributes-natural-language", NULL
, language
->language
);
1564 if (!push_utf8_talloc(frame
, &printername
, lp_printername(talloc_tos(), snum
),
1568 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/printers/%s",
1571 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
1573 if (!push_utf8_talloc(frame
, &username
, current_user_info
.unix_name
, &size
)) {
1576 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
1580 * Do the request and get back a response...
1583 if ((response
= cupsDoRequest(http
, request
, "/admin/")) != NULL
) {
1584 if (ippGetStatusCode(response
) >= IPP_OK_CONFLICT
) {
1585 DEBUG(0,("Unable to resume printer %s - %s\n",
1586 lp_printername(talloc_tos(), snum
),
1587 ippErrorString(cupsLastError())));
1592 DEBUG(0,("Unable to resume printer %s - %s\n",
1593 lp_printername(talloc_tos(), snum
),
1594 ippErrorString(cupsLastError())));
1599 ippDelete(response
);
1602 cupsLangFree(language
);
1611 /*******************************************************************
1612 * CUPS printing interface definitions...
1613 ******************************************************************/
1615 struct printif cups_printif
=
1628 /* this keeps fussy compilers happy */
1629 void print_cups_dummy(void);
1630 void print_cups_dummy(void) {}
1631 #endif /* HAVE_CUPS */