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"
31 #include <cups/cups.h>
32 #include <cups/language.h>
34 #if (CUPS_VERSION_MAJOR > 1) || (CUPS_VERSION_MINOR > 5)
35 #define HAVE_CUPS_1_6 1
39 #define ippGetGroupTag(attr) attr->group_tag
40 #define ippGetName(attr) attr->name
41 #define ippGetValueTag(attr) attr->value_tag
42 #define ippGetStatusCode(ipp) ipp->request.status.status_code
43 #define ippGetInteger(attr, element) attr->values[element].integer
44 #define ippGetString(attr, element, language) attr->values[element].string.text
46 static ipp_attribute_t
*
47 ippFirstAttribute(ipp_t
*ipp
)
51 return (ipp
->current
= ipp
->attrs
);
54 static ipp_attribute_t
*
55 ippNextAttribute(ipp_t
*ipp
)
57 if (!ipp
|| !ipp
->current
)
59 return (ipp
->current
= ipp
->current
->next
);
62 static int ippSetOperation(ipp_t
*ipp
, ipp_op_t op
)
64 ipp
->request
.op
.operation_id
= op
;
68 static int ippSetRequestId(ipp_t
*ipp
, int request_id
)
70 ipp
->request
.any
.request_id
= request_id
;
75 static SIG_ATOMIC_T gotalarm
;
77 /***************************************************************
78 Signal function to tell us we timed out.
79 ****************************************************************/
81 static void gotalarm_sig(int signum
)
86 extern userdom_struct current_user_info
;
89 * 'cups_passwd_cb()' - The CUPS password callback...
92 static const char * /* O - Password or NULL */
93 cups_passwd_cb(const char *prompt
) /* I - Prompt */
96 * Always return NULL to indicate that no password is available...
102 static http_t
*cups_connect(TALLOC_CTX
*frame
)
105 char *server
= NULL
, *p
= NULL
;
107 int timeout
= lp_cups_connection_timeout();
110 if (lp_cups_server(talloc_tos()) != NULL
&& strlen(lp_cups_server(talloc_tos())) > 0) {
111 if (!push_utf8_talloc(frame
, &server
, lp_cups_server(talloc_tos()), &size
)) {
115 server
= talloc_strdup(frame
,cupsServer());
121 p
= strchr(server
, ':');
129 DEBUG(10, ("connecting to cups server %s:%d\n",
135 CatchSignal(SIGALRM
, gotalarm_sig
);
139 #ifdef HAVE_HTTPCONNECTENCRYPT
140 http
= httpConnectEncrypt(server
, port
, lp_cups_encrypt());
142 http
= httpConnect(server
, port
);
146 CatchSignal(SIGALRM
, SIG_IGN
);
150 DEBUG(0,("Unable to connect to CUPS server %s:%d - %s\n",
151 server
, port
, strerror(errno
)));
157 static bool send_pcap_blob(DATA_BLOB
*pcap_blob
, int fd
)
161 ret
= sys_write(fd
, &pcap_blob
->length
, sizeof(pcap_blob
->length
));
162 if (ret
!= sizeof(pcap_blob
->length
)) {
166 ret
= sys_write(fd
, pcap_blob
->data
, pcap_blob
->length
);
167 if (ret
!= pcap_blob
->length
) {
171 DEBUG(10, ("successfully sent blob of len %d\n", (int)ret
));
175 static bool recv_pcap_blob(TALLOC_CTX
*mem_ctx
, int fd
, DATA_BLOB
*pcap_blob
)
180 ret
= sys_read(fd
, &blob_len
, sizeof(blob_len
));
181 if (ret
!= sizeof(blob_len
)) {
185 *pcap_blob
= data_blob_talloc_named(mem_ctx
, NULL
, blob_len
,
187 if (pcap_blob
->length
!= blob_len
) {
190 ret
= sys_read(fd
, pcap_blob
->data
, blob_len
);
191 if (ret
!= blob_len
) {
192 talloc_free(pcap_blob
->data
);
196 DEBUG(10, ("successfully recvd blob of len %d\n", (int)ret
));
200 static bool process_cups_printers_response(TALLOC_CTX
*mem_ctx
,
202 struct pcap_data
*pcap_data
)
204 ipp_attribute_t
*attr
;
207 char *location
= NULL
;
208 struct pcap_printer
*printer
;
211 for (attr
= ippFirstAttribute(response
); attr
!= NULL
;) {
213 * Skip leading attributes until we hit a printer...
216 while (attr
!= NULL
&& ippGetGroupTag(attr
) != IPP_TAG_PRINTER
)
217 attr
= ippNextAttribute(response
);
223 * Pull the needed attributes from this printer...
229 while (attr
!= NULL
&& ippGetGroupTag(attr
) == IPP_TAG_PRINTER
) {
231 if (strcmp(ippGetName(attr
), "printer-name") == 0 &&
232 ippGetValueTag(attr
) == IPP_TAG_NAME
) {
233 if (!pull_utf8_talloc(mem_ctx
,
235 ippGetString(attr
, 0, NULL
),
241 if (strcmp(ippGetName(attr
), "printer-info") == 0 &&
242 ippGetValueTag(attr
) == IPP_TAG_TEXT
) {
243 if (!pull_utf8_talloc(mem_ctx
,
245 ippGetString(attr
, 0, NULL
),
251 if (strcmp(ippGetName(attr
), "printer-location") == 0 &&
252 ippGetValueTag(attr
) == IPP_TAG_TEXT
) {
253 if (!pull_utf8_talloc(mem_ctx
,
255 ippGetString(attr
, 0, NULL
),
261 attr
= ippNextAttribute(response
);
265 * See if we have everything needed...
271 if (pcap_data
->count
== 0) {
272 printer
= talloc_array(mem_ctx
, struct pcap_printer
, 1);
274 printer
= talloc_realloc(mem_ctx
, pcap_data
->printers
,
276 pcap_data
->count
+ 1);
278 if (printer
== NULL
) {
281 pcap_data
->printers
= printer
;
282 pcap_data
->printers
[pcap_data
->count
].name
= name
;
283 pcap_data
->printers
[pcap_data
->count
].info
= info
;
284 pcap_data
->printers
[pcap_data
->count
].location
= location
;
294 * request printer list from cups, send result back to up parent via fd.
295 * returns true if the (possibly failed) result was successfuly sent to parent.
297 static bool cups_cache_reload_async(int fd
)
299 TALLOC_CTX
*frame
= talloc_stackframe();
300 struct pcap_data pcap_data
;
301 http_t
*http
= NULL
; /* HTTP connection to server */
302 ipp_t
*request
= NULL
, /* IPP Request */
303 *response
= NULL
; /* IPP Response */
304 cups_lang_t
*language
= NULL
; /* Default language */
305 static const char *requested
[] =/* Requested attributes */
312 enum ndr_err_code ndr_ret
;
315 ZERO_STRUCT(pcap_data
);
316 pcap_data
.status
= NT_STATUS_UNSUCCESSFUL
;
318 DEBUG(5, ("reloading cups printcap cache\n"));
321 * Make sure we don't ask for passwords...
324 cupsSetPasswordCB(cups_passwd_cb
);
326 if ((http
= cups_connect(frame
)) == NULL
) {
331 * Build a CUPS_GET_PRINTERS request, which requires the following
335 * attributes-natural-language
336 * requested-attributes
341 ippSetOperation(request
, CUPS_GET_PRINTERS
);
342 ippSetRequestId(request
, 1);
344 language
= cupsLangDefault();
346 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
347 "attributes-charset", NULL
, "utf-8");
349 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
350 "attributes-natural-language", NULL
, language
->language
);
352 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
353 "requested-attributes",
354 (sizeof(requested
) / sizeof(requested
[0])),
357 if ((response
= cupsDoRequest(http
, request
, "/")) == NULL
) {
358 DEBUG(0,("Unable to get printer list - %s\n",
359 ippErrorString(cupsLastError())));
363 ret
= process_cups_printers_response(frame
, response
, &pcap_data
);
365 DEBUG(0,("failed to process cups response\n"));
373 * Build a CUPS_GET_CLASSES request, which requires the following
377 * attributes-natural-language
378 * requested-attributes
383 ippSetOperation(request
, CUPS_GET_CLASSES
);
384 ippSetRequestId(request
, 1);
386 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
387 "attributes-charset", NULL
, "utf-8");
389 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
390 "attributes-natural-language", NULL
, language
->language
);
392 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
393 "requested-attributes",
394 (sizeof(requested
) / sizeof(requested
[0])),
397 if ((response
= cupsDoRequest(http
, request
, "/")) == NULL
) {
398 DEBUG(0,("Unable to get printer list - %s\n",
399 ippErrorString(cupsLastError())));
403 ret
= process_cups_printers_response(frame
, response
, &pcap_data
);
405 DEBUG(0,("failed to process cups response\n"));
409 pcap_data
.status
= NT_STATUS_OK
;
415 cupsLangFree(language
);
421 ndr_ret
= ndr_push_struct_blob(&pcap_blob
, frame
, &pcap_data
,
422 (ndr_push_flags_fn_t
)ndr_push_pcap_data
);
423 if (ndr_ret
== NDR_ERR_SUCCESS
) {
424 ret
= send_pcap_blob(&pcap_blob
, fd
);
431 static struct tevent_fd
*cache_fd_event
;
433 static bool cups_pcap_load_async(struct tevent_context
*ev
,
434 struct messaging_context
*msg_ctx
,
443 if (cache_fd_event
) {
444 DEBUG(3,("cups_pcap_load_async: already waiting for "
445 "a refresh event\n" ));
449 DEBUG(5,("cups_pcap_load_async: asynchronously loading cups printers\n"));
451 if (pipe(fds
) == -1) {
456 if (pid
== (pid_t
)-1) {
457 DEBUG(10,("cups_pcap_load_async: fork failed %s\n",
465 DEBUG(10,("cups_pcap_load_async: child pid = %u\n",
466 (unsigned int)pid
));
475 close_all_print_db();
477 status
= reinit_after_fork(msg_ctx
, ev
, true);
478 if (!NT_STATUS_IS_OK(status
)) {
479 DEBUG(0,("cups_pcap_load_async: reinit_after_fork() failed\n"));
480 smb_panic("cups_pcap_load_async: reinit_after_fork() failed");
484 cups_cache_reload_async(fds
[1]);
489 struct cups_async_cb_args
{
491 struct tevent_context
*event_ctx
;
492 struct messaging_context
*msg_ctx
;
493 void (*post_cache_fill_fn
)(struct tevent_context
*,
494 struct messaging_context
*);
497 static void cups_async_callback(struct tevent_context
*event_ctx
,
498 struct tevent_fd
*event
,
502 TALLOC_CTX
*frame
= talloc_stackframe();
503 struct cups_async_cb_args
*cb_args
= (struct cups_async_cb_args
*)p
;
504 struct pcap_cache
*tmp_pcap_cache
= NULL
;
506 struct pcap_data pcap_data
;
508 enum ndr_err_code ndr_ret
;
511 DEBUG(5,("cups_async_callback: callback received for printer data. "
512 "fd = %d\n", cb_args
->pipe_fd
));
514 ret_ok
= recv_pcap_blob(frame
, cb_args
->pipe_fd
, &pcap_blob
);
516 DEBUG(0,("failed to recv pcap blob\n"));
520 ndr_ret
= ndr_pull_struct_blob(&pcap_blob
, frame
, &pcap_data
,
521 (ndr_pull_flags_fn_t
)ndr_pull_pcap_data
);
522 if (ndr_ret
!= NDR_ERR_SUCCESS
) {
526 if (!NT_STATUS_IS_OK(pcap_data
.status
)) {
527 DEBUG(0,("failed to retrieve printer list: %s\n",
528 nt_errstr(pcap_data
.status
)));
532 for (i
= 0; i
< pcap_data
.count
; i
++) {
533 ret_ok
= pcap_cache_add_specific(&tmp_pcap_cache
,
534 pcap_data
.printers
[i
].name
,
535 pcap_data
.printers
[i
].info
,
536 pcap_data
.printers
[i
].location
);
538 DEBUG(0, ("failed to add to tmp pcap cache\n"));
543 /* replace the system-wide pcap cache with a (possibly empty) new one */
544 ret_ok
= pcap_cache_replace(tmp_pcap_cache
);
546 DEBUG(0, ("failed to replace pcap cache\n"));
547 } else if (cb_args
->post_cache_fill_fn
!= NULL
) {
548 /* Caller requested post cache fill callback */
549 cb_args
->post_cache_fill_fn(cb_args
->event_ctx
,
553 pcap_cache_destroy_specific(&tmp_pcap_cache
);
555 close(cb_args
->pipe_fd
);
556 TALLOC_FREE(cb_args
);
557 TALLOC_FREE(cache_fd_event
);
560 bool cups_cache_reload(struct tevent_context
*ev
,
561 struct messaging_context
*msg_ctx
,
562 void (*post_cache_fill_fn
)(struct tevent_context
*,
563 struct messaging_context
*))
565 struct cups_async_cb_args
*cb_args
;
568 cb_args
= talloc(NULL
, struct cups_async_cb_args
);
569 if (cb_args
== NULL
) {
573 cb_args
->post_cache_fill_fn
= post_cache_fill_fn
;
574 cb_args
->event_ctx
= ev
;
575 cb_args
->msg_ctx
= msg_ctx
;
576 p_pipe_fd
= &cb_args
->pipe_fd
;
579 /* Set up an async refresh. */
580 if (!cups_pcap_load_async(ev
, msg_ctx
, p_pipe_fd
)) {
581 talloc_free(cb_args
);
585 DEBUG(10,("cups_cache_reload: async read on fd %d\n",
588 /* Trigger an event when the pipe can be read. */
589 cache_fd_event
= tevent_add_fd(ev
,
594 if (!cache_fd_event
) {
596 TALLOC_FREE(cb_args
);
604 * 'cups_job_delete()' - Delete a job.
607 static int cups_job_delete(const char *sharename
, const char *lprm_command
, struct printjob
*pjob
)
609 TALLOC_CTX
*frame
= talloc_stackframe();
610 int ret
= 1; /* Return value */
611 http_t
*http
= NULL
; /* HTTP connection to server */
612 ipp_t
*request
= NULL
, /* IPP Request */
613 *response
= NULL
; /* IPP Response */
614 cups_lang_t
*language
= NULL
; /* Default language */
616 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
619 DEBUG(5,("cups_job_delete(%s, %p (%d))\n", sharename
, pjob
, pjob
->sysjob
));
622 * Make sure we don't ask for passwords...
625 cupsSetPasswordCB(cups_passwd_cb
);
628 * Try to connect to the server...
631 if ((http
= cups_connect(frame
)) == NULL
) {
636 * Build an IPP_CANCEL_JOB request, which requires the following
640 * attributes-natural-language
642 * requesting-user-name
647 ippSetOperation(request
, IPP_CANCEL_JOB
);
648 ippSetRequestId(request
, 1);
650 language
= cupsLangDefault();
652 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
653 "attributes-charset", NULL
, "utf-8");
655 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
656 "attributes-natural-language", NULL
, language
->language
);
658 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/jobs/%d", pjob
->sysjob
);
660 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
662 if (!push_utf8_talloc(frame
, &user
, pjob
->user
, &size
)) {
666 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
670 * Do the request and get back a response...
673 if ((response
= cupsDoRequest(http
, request
, "/jobs")) != NULL
) {
674 if (ippGetStatusCode(response
) >= IPP_OK_CONFLICT
) {
675 DEBUG(0,("Unable to cancel job %d - %s\n", pjob
->sysjob
,
676 ippErrorString(cupsLastError())));
681 DEBUG(0,("Unable to cancel job %d - %s\n", pjob
->sysjob
,
682 ippErrorString(cupsLastError())));
690 cupsLangFree(language
);
701 * 'cups_job_pause()' - Pause a job.
704 static int cups_job_pause(int snum
, struct printjob
*pjob
)
706 TALLOC_CTX
*frame
= talloc_stackframe();
707 int ret
= 1; /* Return value */
708 http_t
*http
= NULL
; /* HTTP connection to server */
709 ipp_t
*request
= NULL
, /* IPP Request */
710 *response
= NULL
; /* IPP Response */
711 cups_lang_t
*language
= NULL
; /* Default language */
713 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
716 DEBUG(5,("cups_job_pause(%d, %p (%d))\n", snum
, pjob
, pjob
->sysjob
));
719 * Make sure we don't ask for passwords...
722 cupsSetPasswordCB(cups_passwd_cb
);
725 * Try to connect to the server...
728 if ((http
= cups_connect(frame
)) == NULL
) {
733 * Build an IPP_HOLD_JOB request, which requires the following
737 * attributes-natural-language
739 * requesting-user-name
744 ippSetOperation(request
, IPP_HOLD_JOB
);
745 ippSetRequestId(request
, 1);
747 language
= cupsLangDefault();
749 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
750 "attributes-charset", NULL
, "utf-8");
752 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
753 "attributes-natural-language", NULL
, language
->language
);
755 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/jobs/%d", pjob
->sysjob
);
757 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
759 if (!push_utf8_talloc(frame
, &user
, pjob
->user
, &size
)) {
762 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
766 * Do the request and get back a response...
769 if ((response
= cupsDoRequest(http
, request
, "/jobs")) != NULL
) {
770 if (ippGetStatusCode(response
) >= IPP_OK_CONFLICT
) {
771 DEBUG(0,("Unable to hold job %d - %s\n", pjob
->sysjob
,
772 ippErrorString(cupsLastError())));
777 DEBUG(0,("Unable to hold job %d - %s\n", pjob
->sysjob
,
778 ippErrorString(cupsLastError())));
786 cupsLangFree(language
);
797 * 'cups_job_resume()' - Resume a paused job.
800 static int cups_job_resume(int snum
, struct printjob
*pjob
)
802 TALLOC_CTX
*frame
= talloc_stackframe();
803 int ret
= 1; /* Return value */
804 http_t
*http
= NULL
; /* HTTP connection to server */
805 ipp_t
*request
= NULL
, /* IPP Request */
806 *response
= NULL
; /* IPP Response */
807 cups_lang_t
*language
= NULL
; /* Default language */
809 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
812 DEBUG(5,("cups_job_resume(%d, %p (%d))\n", snum
, pjob
, pjob
->sysjob
));
815 * Make sure we don't ask for passwords...
818 cupsSetPasswordCB(cups_passwd_cb
);
821 * Try to connect to the server...
824 if ((http
= cups_connect(frame
)) == NULL
) {
829 * Build an IPP_RELEASE_JOB request, which requires the following
833 * attributes-natural-language
835 * requesting-user-name
840 ippSetOperation(request
, IPP_RELEASE_JOB
);
841 ippSetRequestId(request
, 1);
843 language
= cupsLangDefault();
845 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
846 "attributes-charset", NULL
, "utf-8");
848 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
849 "attributes-natural-language", NULL
, language
->language
);
851 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/jobs/%d", pjob
->sysjob
);
853 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
855 if (!push_utf8_talloc(frame
, &user
, pjob
->user
, &size
)) {
858 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
862 * Do the request and get back a response...
865 if ((response
= cupsDoRequest(http
, request
, "/jobs")) != NULL
) {
866 if (ippGetStatusCode(response
) >= IPP_OK_CONFLICT
) {
867 DEBUG(0,("Unable to release job %d - %s\n", pjob
->sysjob
,
868 ippErrorString(cupsLastError())));
873 DEBUG(0,("Unable to release job %d - %s\n", pjob
->sysjob
,
874 ippErrorString(cupsLastError())));
882 cupsLangFree(language
);
893 * 'cups_job_submit()' - Submit a job for printing.
896 static int cups_job_submit(int snum
, struct printjob
*pjob
,
897 enum printing_types printing_type
,
900 TALLOC_CTX
*frame
= talloc_stackframe();
901 int ret
= 1; /* Return value */
902 http_t
*http
= NULL
; /* HTTP connection to server */
903 ipp_t
*request
= NULL
, /* IPP Request */
904 *response
= NULL
; /* IPP Response */
905 ipp_attribute_t
*attr_job_id
= NULL
; /* IPP Attribute "job-id" */
906 cups_lang_t
*language
= NULL
; /* Default language */
907 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
908 char *new_jobname
= NULL
;
910 cups_option_t
*options
= NULL
;
911 char *printername
= NULL
;
913 char *jobname
= NULL
;
914 char *cupsoptions
= NULL
;
915 char *filename
= NULL
;
918 DEBUG(5,("cups_job_submit(%d, %p)\n", snum
, pjob
));
921 * Make sure we don't ask for passwords...
924 cupsSetPasswordCB(cups_passwd_cb
);
927 * Try to connect to the server...
930 if ((http
= cups_connect(frame
)) == NULL
) {
935 * Build an IPP_PRINT_JOB request, which requires the following
939 * attributes-natural-language
941 * requesting-user-name
947 ippSetOperation(request
, IPP_PRINT_JOB
);
948 ippSetRequestId(request
, 1);
950 language
= cupsLangDefault();
952 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
953 "attributes-charset", NULL
, "utf-8");
955 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
956 "attributes-natural-language", NULL
, language
->language
);
958 if (!push_utf8_talloc(frame
, &printername
,
959 lp_printername(talloc_tos(), snum
),
963 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/printers/%s",
966 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
967 "printer-uri", NULL
, uri
);
969 if (!push_utf8_talloc(frame
, &user
, pjob
->user
, &size
)) {
972 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
975 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
976 "job-originating-host-name", NULL
,
977 pjob
->clientmachine
);
979 if (!push_utf8_talloc(frame
, &jobname
, pjob
->jobname
, &size
)) {
982 new_jobname
= talloc_asprintf(frame
,
983 "%s%.8u %s", PRINT_SPOOL_PREFIX
,
984 pjob
->jobid
, jobname
);
985 if (new_jobname
== NULL
) {
989 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name", NULL
,
993 * add any options defined in smb.conf
996 if (!push_utf8_talloc(frame
, &cupsoptions
,
997 lp_cups_options(talloc_tos(), snum
), &size
)) {
1002 num_options
= cupsParseOptions(cupsoptions
, num_options
, &options
);
1005 cupsEncodeOptions(request
, num_options
, options
);
1008 * Do the request and get back a response...
1011 slprintf(uri
, sizeof(uri
) - 1, "/printers/%s", printername
);
1013 if (!push_utf8_talloc(frame
, &filename
, pjob
->filename
, &size
)) {
1016 if ((response
= cupsDoFileRequest(http
, request
, uri
, pjob
->filename
)) != NULL
) {
1017 if (ippGetStatusCode(response
) >= IPP_OK_CONFLICT
) {
1018 DEBUG(0,("Unable to print file to %s - %s\n",
1019 lp_printername(talloc_tos(), snum
),
1020 ippErrorString(cupsLastError())));
1023 attr_job_id
= ippFindAttribute(response
, "job-id", IPP_TAG_INTEGER
);
1025 pjob
->sysjob
= ippGetInteger(attr_job_id
, 0);
1026 DEBUG(5,("cups_job_submit: job-id %d\n", pjob
->sysjob
));
1028 DEBUG(0,("Missing job-id attribute in IPP response"));
1032 DEBUG(0,("Unable to print file to `%s' - %s\n",
1033 lp_printername(talloc_tos(), snum
),
1034 ippErrorString(cupsLastError())));
1038 unlink(pjob
->filename
);
1039 /* else print_job_end will do it for us */
1043 ippDelete(response
);
1046 cupsLangFree(language
);
1057 * 'cups_queue_get()' - Get all the jobs in the print queue.
1060 static int cups_queue_get(const char *sharename
,
1061 enum printing_types printing_type
,
1063 print_queue_struct
**q
,
1064 print_status_struct
*status
)
1066 TALLOC_CTX
*frame
= talloc_stackframe();
1067 char *printername
= NULL
;
1068 http_t
*http
= NULL
; /* HTTP connection to server */
1069 ipp_t
*request
= NULL
, /* IPP Request */
1070 *response
= NULL
; /* IPP Response */
1071 ipp_attribute_t
*attr
= NULL
; /* Current attribute */
1072 cups_lang_t
*language
= NULL
; /* Default language */
1073 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
1074 int qcount
= 0, /* Number of active queue entries */
1075 qalloc
= 0; /* Number of queue entries allocated */
1076 print_queue_struct
*queue
= NULL
, /* Queue entries */
1077 *temp
; /* Temporary pointer for queue */
1078 char *user_name
= NULL
, /* job-originating-user-name attribute */
1079 *job_name
= NULL
; /* job-name attribute */
1080 int job_id
; /* job-id attribute */
1081 int job_k_octets
; /* job-k-octets attribute */
1082 time_t job_time
; /* time-at-creation attribute */
1083 ipp_jstate_t job_status
; /* job-status attribute */
1084 int job_priority
; /* job-priority attribute */
1086 static const char *jattrs
[] = /* Requested job attributes */
1091 "job-originating-user-name",
1096 static const char *pattrs
[] = /* Requested printer attributes */
1099 "printer-state-message"
1104 /* HACK ALERT!!! The problem with support the 'printer name'
1105 option is that we key the tdb off the sharename. So we will
1106 overload the lpq_command string to pass in the printername
1107 (which is basically what we do for non-cups printers ... using
1108 the lpq_command to get the queue listing). */
1110 if (!push_utf8_talloc(frame
, &printername
, lpq_command
, &size
)) {
1113 DEBUG(5,("cups_queue_get(%s, %p, %p)\n", lpq_command
, q
, status
));
1116 * Make sure we don't ask for passwords...
1119 cupsSetPasswordCB(cups_passwd_cb
);
1122 * Try to connect to the server...
1125 if ((http
= cups_connect(frame
)) == NULL
) {
1130 * Generate the printer URI...
1133 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/printers/%s", printername
);
1136 * Build an IPP_GET_JOBS request, which requires the following
1139 * attributes-charset
1140 * attributes-natural-language
1141 * requested-attributes
1147 ippSetOperation(request
, IPP_GET_JOBS
);
1148 ippSetRequestId(request
, 1);
1150 language
= cupsLangDefault();
1152 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1153 "attributes-charset", NULL
, "utf-8");
1155 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1156 "attributes-natural-language", NULL
, language
->language
);
1158 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1159 "requested-attributes",
1160 (sizeof(jattrs
) / sizeof(jattrs
[0])),
1163 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1164 "printer-uri", NULL
, uri
);
1167 * Do the request and get back a response...
1170 if ((response
= cupsDoRequest(http
, request
, "/")) == NULL
) {
1171 DEBUG(0,("Unable to get jobs for %s - %s\n", uri
,
1172 ippErrorString(cupsLastError())));
1176 if (ippGetStatusCode(response
) >= IPP_OK_CONFLICT
) {
1177 DEBUG(0,("Unable to get jobs for %s - %s\n", uri
,
1178 ippErrorString(ippGetStatusCode(response
))));
1183 * Process the jobs...
1190 for (attr
= ippFirstAttribute(response
); attr
!= NULL
; attr
= ippNextAttribute(response
)) {
1192 * Skip leading attributes until we hit a job...
1195 while (attr
!= NULL
&& ippGetGroupTag(attr
) != IPP_TAG_JOB
)
1196 attr
= ippNextAttribute(response
);
1202 * Allocate memory as needed...
1204 if (qcount
>= qalloc
) {
1207 queue
= SMB_REALLOC_ARRAY(queue
, print_queue_struct
, qalloc
);
1209 if (queue
== NULL
) {
1210 DEBUG(0,("cups_queue_get: Not enough memory!"));
1216 temp
= queue
+ qcount
;
1217 memset(temp
, 0, sizeof(print_queue_struct
));
1220 * Pull the needed attributes from this job...
1225 job_status
= IPP_JOB_PENDING
;
1231 while (attr
!= NULL
&& ippGetGroupTag(attr
) == IPP_TAG_JOB
) {
1232 if (ippGetName(attr
) == NULL
) {
1233 attr
= ippNextAttribute(response
);
1237 if (strcmp(ippGetName(attr
), "job-id") == 0 &&
1238 ippGetValueTag(attr
) == IPP_TAG_INTEGER
)
1239 job_id
= ippGetInteger(attr
, 0);
1241 if (strcmp(ippGetName(attr
), "job-k-octets") == 0 &&
1242 ippGetValueTag(attr
) == IPP_TAG_INTEGER
)
1243 job_k_octets
= ippGetInteger(attr
, 0);
1245 if (strcmp(ippGetName(attr
), "job-priority") == 0 &&
1246 ippGetValueTag(attr
) == IPP_TAG_INTEGER
)
1247 job_priority
= ippGetInteger(attr
, 0);
1249 if (strcmp(ippGetName(attr
), "job-state") == 0 &&
1250 ippGetValueTag(attr
) == IPP_TAG_ENUM
)
1251 job_status
= (ipp_jstate_t
)ippGetInteger(attr
, 0);
1253 if (strcmp(ippGetName(attr
), "time-at-creation") == 0 &&
1254 ippGetValueTag(attr
) == IPP_TAG_INTEGER
)
1255 job_time
= ippGetInteger(attr
, 0);
1257 if (strcmp(ippGetName(attr
), "job-name") == 0 &&
1258 ippGetValueTag(attr
) == IPP_TAG_NAME
) {
1259 if (!pull_utf8_talloc(frame
,
1261 ippGetString(attr
, 0, NULL
),
1267 if (strcmp(ippGetName(attr
), "job-originating-user-name") == 0 &&
1268 ippGetValueTag(attr
) == IPP_TAG_NAME
) {
1269 if (!pull_utf8_talloc(frame
,
1271 ippGetString(attr
, 0, NULL
),
1277 attr
= ippNextAttribute(response
);
1281 * See if we have everything needed...
1284 if (user_name
== NULL
|| job_name
== NULL
|| job_id
== 0) {
1291 temp
->sysjob
= job_id
;
1292 temp
->size
= job_k_octets
* 1024;
1293 temp
->status
= job_status
== IPP_JOB_PENDING
? LPQ_QUEUED
:
1294 job_status
== IPP_JOB_STOPPED
? LPQ_PAUSED
:
1295 job_status
== IPP_JOB_HELD
? LPQ_PAUSED
:
1297 temp
->priority
= job_priority
;
1298 temp
->time
= job_time
;
1299 strlcpy(temp
->fs_user
, user_name
, sizeof(temp
->fs_user
));
1300 strlcpy(temp
->fs_file
, job_name
, sizeof(temp
->fs_file
));
1308 ippDelete(response
);
1312 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the
1313 * following attributes:
1315 * attributes-charset
1316 * attributes-natural-language
1317 * requested-attributes
1323 ippSetOperation(request
, IPP_GET_PRINTER_ATTRIBUTES
);
1324 ippSetRequestId(request
, 1);
1326 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1327 "attributes-charset", NULL
, "utf-8");
1329 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1330 "attributes-natural-language", NULL
, language
->language
);
1332 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1333 "requested-attributes",
1334 (sizeof(pattrs
) / sizeof(pattrs
[0])),
1337 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1338 "printer-uri", NULL
, uri
);
1341 * Do the request and get back a response...
1344 if ((response
= cupsDoRequest(http
, request
, "/")) == NULL
) {
1345 DEBUG(0,("Unable to get printer status for %s - %s\n", printername
,
1346 ippErrorString(cupsLastError())));
1350 if (ippGetStatusCode(response
) >= IPP_OK_CONFLICT
) {
1351 DEBUG(0,("Unable to get printer status for %s - %s\n", printername
,
1352 ippErrorString(ippGetStatusCode(response
))));
1357 * Get the current printer status and convert it to the SAMBA values.
1360 if ((attr
= ippFindAttribute(response
, "printer-state", IPP_TAG_ENUM
)) != NULL
) {
1361 if (ippGetInteger(attr
, 0) == IPP_PRINTER_STOPPED
)
1362 status
->status
= LPSTAT_STOPPED
;
1364 status
->status
= LPSTAT_OK
;
1367 if ((attr
= ippFindAttribute(response
, "printer-state-message",
1368 IPP_TAG_TEXT
)) != NULL
) {
1370 if (!pull_utf8_talloc(frame
, &msg
,
1371 ippGetString(attr
, 0, NULL
),
1377 fstrcpy(status
->message
, msg
);
1383 * Return the job queue...
1389 ippDelete(response
);
1392 cupsLangFree(language
);
1403 * 'cups_queue_pause()' - Pause a print queue.
1406 static int cups_queue_pause(int snum
)
1408 TALLOC_CTX
*frame
= talloc_stackframe();
1409 int ret
= 1; /* Return value */
1410 http_t
*http
= NULL
; /* HTTP connection to server */
1411 ipp_t
*request
= NULL
, /* IPP Request */
1412 *response
= NULL
; /* IPP Response */
1413 cups_lang_t
*language
= NULL
; /* Default language */
1414 char *printername
= NULL
;
1415 char *username
= NULL
;
1416 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
1419 DEBUG(5,("cups_queue_pause(%d)\n", snum
));
1422 * Make sure we don't ask for passwords...
1425 cupsSetPasswordCB(cups_passwd_cb
);
1428 * Try to connect to the server...
1431 if ((http
= cups_connect(frame
)) == NULL
) {
1436 * Build an IPP_PAUSE_PRINTER request, which requires the following
1439 * attributes-charset
1440 * attributes-natural-language
1442 * requesting-user-name
1447 ippSetOperation(request
, IPP_PAUSE_PRINTER
);
1448 ippSetRequestId(request
, 1);
1450 language
= cupsLangDefault();
1452 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1453 "attributes-charset", NULL
, "utf-8");
1455 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1456 "attributes-natural-language", NULL
, language
->language
);
1458 if (!push_utf8_talloc(frame
, &printername
,
1459 lp_printername(talloc_tos(), snum
), &size
)) {
1462 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/printers/%s",
1465 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
1467 if (!push_utf8_talloc(frame
, &username
, current_user_info
.unix_name
, &size
)) {
1470 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
1474 * Do the request and get back a response...
1477 if ((response
= cupsDoRequest(http
, request
, "/admin/")) != NULL
) {
1478 if (ippGetStatusCode(response
) >= IPP_OK_CONFLICT
) {
1479 DEBUG(0,("Unable to pause printer %s - %s\n",
1480 lp_printername(talloc_tos(), snum
),
1481 ippErrorString(cupsLastError())));
1486 DEBUG(0,("Unable to pause printer %s - %s\n",
1487 lp_printername(talloc_tos(), snum
),
1488 ippErrorString(cupsLastError())));
1493 ippDelete(response
);
1496 cupsLangFree(language
);
1507 * 'cups_queue_resume()' - Restart a print queue.
1510 static int cups_queue_resume(int snum
)
1512 TALLOC_CTX
*frame
= talloc_stackframe();
1513 int ret
= 1; /* Return value */
1514 http_t
*http
= NULL
; /* HTTP connection to server */
1515 ipp_t
*request
= NULL
, /* IPP Request */
1516 *response
= NULL
; /* IPP Response */
1517 cups_lang_t
*language
= NULL
; /* Default language */
1518 char *printername
= NULL
;
1519 char *username
= NULL
;
1520 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
1523 DEBUG(5,("cups_queue_resume(%d)\n", snum
));
1526 * Make sure we don't ask for passwords...
1529 cupsSetPasswordCB(cups_passwd_cb
);
1532 * Try to connect to the server...
1535 if ((http
= cups_connect(frame
)) == NULL
) {
1540 * Build an IPP_RESUME_PRINTER request, which requires the following
1543 * attributes-charset
1544 * attributes-natural-language
1546 * requesting-user-name
1551 ippSetOperation(request
, IPP_RESUME_PRINTER
);
1552 ippSetRequestId(request
, 1);
1554 language
= cupsLangDefault();
1556 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1557 "attributes-charset", NULL
, "utf-8");
1559 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1560 "attributes-natural-language", NULL
, language
->language
);
1562 if (!push_utf8_talloc(frame
, &printername
, lp_printername(talloc_tos(), snum
),
1566 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/printers/%s",
1569 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
1571 if (!push_utf8_talloc(frame
, &username
, current_user_info
.unix_name
, &size
)) {
1574 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
1578 * Do the request and get back a response...
1581 if ((response
= cupsDoRequest(http
, request
, "/admin/")) != NULL
) {
1582 if (ippGetStatusCode(response
) >= IPP_OK_CONFLICT
) {
1583 DEBUG(0,("Unable to resume printer %s - %s\n",
1584 lp_printername(talloc_tos(), snum
),
1585 ippErrorString(cupsLastError())));
1590 DEBUG(0,("Unable to resume printer %s - %s\n",
1591 lp_printername(talloc_tos(), snum
),
1592 ippErrorString(cupsLastError())));
1597 ippDelete(response
);
1600 cupsLangFree(language
);
1609 /*******************************************************************
1610 * CUPS printing interface definitions...
1611 ******************************************************************/
1613 struct printif cups_printif
=
1626 /* this keeps fussy compilers happy */
1627 void print_cups_dummy(void);
1628 void print_cups_dummy(void) {}
1629 #endif /* HAVE_CUPS */