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() != NULL
&& strlen(lp_cups_server()) > 0) {
111 if (!push_utf8_talloc(frame
, &server
, lp_cups_server(), &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 fd_event
*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
, procid_self(), 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 event_context
*event_ctx
;
492 struct messaging_context
*msg_ctx
;
493 void (*post_cache_fill_fn
)(struct event_context
*,
494 struct messaging_context
*);
497 static void cups_async_callback(struct event_context
*event_ctx
,
498 struct fd_event
*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_P(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
= event_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
, lp_printername(snum
),
962 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/printers/%s",
965 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
966 "printer-uri", NULL
, uri
);
968 if (!push_utf8_talloc(frame
, &user
, pjob
->user
, &size
)) {
971 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
974 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
975 "job-originating-host-name", NULL
,
976 pjob
->clientmachine
);
978 if (!push_utf8_talloc(frame
, &jobname
, pjob
->jobname
, &size
)) {
981 new_jobname
= talloc_asprintf(frame
,
982 "%s%.8u %s", PRINT_SPOOL_PREFIX
,
983 pjob
->jobid
, jobname
);
984 if (new_jobname
== NULL
) {
988 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name", NULL
,
992 * add any options defined in smb.conf
995 if (!push_utf8_talloc(frame
, &cupsoptions
, lp_cups_options(snum
), &size
)) {
1000 num_options
= cupsParseOptions(cupsoptions
, num_options
, &options
);
1003 cupsEncodeOptions(request
, num_options
, options
);
1006 * Do the request and get back a response...
1009 slprintf(uri
, sizeof(uri
) - 1, "/printers/%s", printername
);
1011 if (!push_utf8_talloc(frame
, &filename
, pjob
->filename
, &size
)) {
1014 if ((response
= cupsDoFileRequest(http
, request
, uri
, pjob
->filename
)) != NULL
) {
1015 if (ippGetStatusCode(response
) >= IPP_OK_CONFLICT
) {
1016 DEBUG(0,("Unable to print file to %s - %s\n",
1017 lp_printername(snum
),
1018 ippErrorString(cupsLastError())));
1021 attr_job_id
= ippFindAttribute(response
, "job-id", IPP_TAG_INTEGER
);
1023 pjob
->sysjob
= ippGetInteger(attr_job_id
, 0);
1024 DEBUG(5,("cups_job_submit: job-id %d\n", pjob
->sysjob
));
1026 DEBUG(0,("Missing job-id attribute in IPP response"));
1030 DEBUG(0,("Unable to print file to `%s' - %s\n",
1031 lp_printername(snum
),
1032 ippErrorString(cupsLastError())));
1036 unlink(pjob
->filename
);
1037 /* else print_job_end will do it for us */
1041 ippDelete(response
);
1044 cupsLangFree(language
);
1055 * 'cups_queue_get()' - Get all the jobs in the print queue.
1058 static int cups_queue_get(const char *sharename
,
1059 enum printing_types printing_type
,
1061 print_queue_struct
**q
,
1062 print_status_struct
*status
)
1064 TALLOC_CTX
*frame
= talloc_stackframe();
1065 char *printername
= NULL
;
1066 http_t
*http
= NULL
; /* HTTP connection to server */
1067 ipp_t
*request
= NULL
, /* IPP Request */
1068 *response
= NULL
; /* IPP Response */
1069 ipp_attribute_t
*attr
= NULL
; /* Current attribute */
1070 cups_lang_t
*language
= NULL
; /* Default language */
1071 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
1072 int qcount
= 0, /* Number of active queue entries */
1073 qalloc
= 0; /* Number of queue entries allocated */
1074 print_queue_struct
*queue
= NULL
, /* Queue entries */
1075 *temp
; /* Temporary pointer for queue */
1076 char *user_name
= NULL
, /* job-originating-user-name attribute */
1077 *job_name
= NULL
; /* job-name attribute */
1078 int job_id
; /* job-id attribute */
1079 int job_k_octets
; /* job-k-octets attribute */
1080 time_t job_time
; /* time-at-creation attribute */
1081 ipp_jstate_t job_status
; /* job-status attribute */
1082 int job_priority
; /* job-priority attribute */
1084 static const char *jattrs
[] = /* Requested job attributes */
1089 "job-originating-user-name",
1094 static const char *pattrs
[] = /* Requested printer attributes */
1097 "printer-state-message"
1102 /* HACK ALERT!!! The problem with support the 'printer name'
1103 option is that we key the tdb off the sharename. So we will
1104 overload the lpq_command string to pass in the printername
1105 (which is basically what we do for non-cups printers ... using
1106 the lpq_command to get the queue listing). */
1108 if (!push_utf8_talloc(frame
, &printername
, lpq_command
, &size
)) {
1111 DEBUG(5,("cups_queue_get(%s, %p, %p)\n", lpq_command
, q
, status
));
1114 * Make sure we don't ask for passwords...
1117 cupsSetPasswordCB(cups_passwd_cb
);
1120 * Try to connect to the server...
1123 if ((http
= cups_connect(frame
)) == NULL
) {
1128 * Generate the printer URI...
1131 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/printers/%s", printername
);
1134 * Build an IPP_GET_JOBS request, which requires the following
1137 * attributes-charset
1138 * attributes-natural-language
1139 * requested-attributes
1145 ippSetOperation(request
, IPP_GET_JOBS
);
1146 ippSetRequestId(request
, 1);
1148 language
= cupsLangDefault();
1150 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1151 "attributes-charset", NULL
, "utf-8");
1153 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1154 "attributes-natural-language", NULL
, language
->language
);
1156 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1157 "requested-attributes",
1158 (sizeof(jattrs
) / sizeof(jattrs
[0])),
1161 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1162 "printer-uri", NULL
, uri
);
1165 * Do the request and get back a response...
1168 if ((response
= cupsDoRequest(http
, request
, "/")) == NULL
) {
1169 DEBUG(0,("Unable to get jobs for %s - %s\n", uri
,
1170 ippErrorString(cupsLastError())));
1174 if (ippGetStatusCode(response
) >= IPP_OK_CONFLICT
) {
1175 DEBUG(0,("Unable to get jobs for %s - %s\n", uri
,
1176 ippErrorString(ippGetStatusCode(response
))));
1181 * Process the jobs...
1188 for (attr
= ippFirstAttribute(response
); attr
!= NULL
; attr
= ippNextAttribute(response
)) {
1190 * Skip leading attributes until we hit a job...
1193 while (attr
!= NULL
&& ippGetGroupTag(attr
) != IPP_TAG_JOB
)
1194 attr
= ippNextAttribute(response
);
1200 * Allocate memory as needed...
1202 if (qcount
>= qalloc
) {
1205 queue
= SMB_REALLOC_ARRAY(queue
, print_queue_struct
, qalloc
);
1207 if (queue
== NULL
) {
1208 DEBUG(0,("cups_queue_get: Not enough memory!"));
1214 temp
= queue
+ qcount
;
1215 memset(temp
, 0, sizeof(print_queue_struct
));
1218 * Pull the needed attributes from this job...
1223 job_status
= IPP_JOB_PENDING
;
1229 while (attr
!= NULL
&& ippGetGroupTag(attr
) == IPP_TAG_JOB
) {
1230 if (ippGetName(attr
) == NULL
) {
1231 attr
= ippNextAttribute(response
);
1235 if (strcmp(ippGetName(attr
), "job-id") == 0 &&
1236 ippGetValueTag(attr
) == IPP_TAG_INTEGER
)
1237 job_id
= ippGetInteger(attr
, 0);
1239 if (strcmp(ippGetName(attr
), "job-k-octets") == 0 &&
1240 ippGetValueTag(attr
) == IPP_TAG_INTEGER
)
1241 job_k_octets
= ippGetInteger(attr
, 0);
1243 if (strcmp(ippGetName(attr
), "job-priority") == 0 &&
1244 ippGetValueTag(attr
) == IPP_TAG_INTEGER
)
1245 job_priority
= ippGetInteger(attr
, 0);
1247 if (strcmp(ippGetName(attr
), "job-state") == 0 &&
1248 ippGetValueTag(attr
) == IPP_TAG_ENUM
)
1249 job_status
= (ipp_jstate_t
)ippGetInteger(attr
, 0);
1251 if (strcmp(ippGetName(attr
), "time-at-creation") == 0 &&
1252 ippGetValueTag(attr
) == IPP_TAG_INTEGER
)
1253 job_time
= ippGetInteger(attr
, 0);
1255 if (strcmp(ippGetName(attr
), "job-name") == 0 &&
1256 ippGetValueTag(attr
) == IPP_TAG_NAME
) {
1257 if (!pull_utf8_talloc(frame
,
1259 ippGetString(attr
, 0, NULL
),
1265 if (strcmp(ippGetName(attr
), "job-originating-user-name") == 0 &&
1266 ippGetValueTag(attr
) == IPP_TAG_NAME
) {
1267 if (!pull_utf8_talloc(frame
,
1269 ippGetString(attr
, 0, NULL
),
1275 attr
= ippNextAttribute(response
);
1279 * See if we have everything needed...
1282 if (user_name
== NULL
|| job_name
== NULL
|| job_id
== 0) {
1289 temp
->sysjob
= job_id
;
1290 temp
->size
= job_k_octets
* 1024;
1291 temp
->status
= job_status
== IPP_JOB_PENDING
? LPQ_QUEUED
:
1292 job_status
== IPP_JOB_STOPPED
? LPQ_PAUSED
:
1293 job_status
== IPP_JOB_HELD
? LPQ_PAUSED
:
1295 temp
->priority
= job_priority
;
1296 temp
->time
= job_time
;
1297 strlcpy(temp
->fs_user
, user_name
, sizeof(temp
->fs_user
));
1298 strlcpy(temp
->fs_file
, job_name
, sizeof(temp
->fs_file
));
1306 ippDelete(response
);
1310 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the
1311 * following attributes:
1313 * attributes-charset
1314 * attributes-natural-language
1315 * requested-attributes
1321 ippSetOperation(request
, IPP_GET_PRINTER_ATTRIBUTES
);
1322 ippSetRequestId(request
, 1);
1324 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1325 "attributes-charset", NULL
, "utf-8");
1327 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1328 "attributes-natural-language", NULL
, language
->language
);
1330 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1331 "requested-attributes",
1332 (sizeof(pattrs
) / sizeof(pattrs
[0])),
1335 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1336 "printer-uri", NULL
, uri
);
1339 * Do the request and get back a response...
1342 if ((response
= cupsDoRequest(http
, request
, "/")) == NULL
) {
1343 DEBUG(0,("Unable to get printer status for %s - %s\n", printername
,
1344 ippErrorString(cupsLastError())));
1348 if (ippGetStatusCode(response
) >= IPP_OK_CONFLICT
) {
1349 DEBUG(0,("Unable to get printer status for %s - %s\n", printername
,
1350 ippErrorString(ippGetStatusCode(response
))));
1355 * Get the current printer status and convert it to the SAMBA values.
1358 if ((attr
= ippFindAttribute(response
, "printer-state", IPP_TAG_ENUM
)) != NULL
) {
1359 if (ippGetInteger(attr
, 0) == IPP_PRINTER_STOPPED
)
1360 status
->status
= LPSTAT_STOPPED
;
1362 status
->status
= LPSTAT_OK
;
1365 if ((attr
= ippFindAttribute(response
, "printer-state-message",
1366 IPP_TAG_TEXT
)) != NULL
) {
1368 if (!pull_utf8_talloc(frame
, &msg
,
1369 ippGetString(attr
, 0, NULL
),
1375 fstrcpy(status
->message
, msg
);
1381 * Return the job queue...
1387 ippDelete(response
);
1390 cupsLangFree(language
);
1401 * 'cups_queue_pause()' - Pause a print queue.
1404 static int cups_queue_pause(int snum
)
1406 TALLOC_CTX
*frame
= talloc_stackframe();
1407 int ret
= 1; /* Return value */
1408 http_t
*http
= NULL
; /* HTTP connection to server */
1409 ipp_t
*request
= NULL
, /* IPP Request */
1410 *response
= NULL
; /* IPP Response */
1411 cups_lang_t
*language
= NULL
; /* Default language */
1412 char *printername
= NULL
;
1413 char *username
= NULL
;
1414 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
1417 DEBUG(5,("cups_queue_pause(%d)\n", snum
));
1420 * Make sure we don't ask for passwords...
1423 cupsSetPasswordCB(cups_passwd_cb
);
1426 * Try to connect to the server...
1429 if ((http
= cups_connect(frame
)) == NULL
) {
1434 * Build an IPP_PAUSE_PRINTER request, which requires the following
1437 * attributes-charset
1438 * attributes-natural-language
1440 * requesting-user-name
1445 ippSetOperation(request
, IPP_PAUSE_PRINTER
);
1446 ippSetRequestId(request
, 1);
1448 language
= cupsLangDefault();
1450 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1451 "attributes-charset", NULL
, "utf-8");
1453 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1454 "attributes-natural-language", NULL
, language
->language
);
1456 if (!push_utf8_talloc(frame
, &printername
, lp_printername(snum
),
1460 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/printers/%s",
1463 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
1465 if (!push_utf8_talloc(frame
, &username
, current_user_info
.unix_name
, &size
)) {
1468 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
1472 * Do the request and get back a response...
1475 if ((response
= cupsDoRequest(http
, request
, "/admin/")) != NULL
) {
1476 if (ippGetStatusCode(response
) >= IPP_OK_CONFLICT
) {
1477 DEBUG(0,("Unable to pause printer %s - %s\n",
1478 lp_printername(snum
),
1479 ippErrorString(cupsLastError())));
1484 DEBUG(0,("Unable to pause printer %s - %s\n",
1485 lp_printername(snum
),
1486 ippErrorString(cupsLastError())));
1491 ippDelete(response
);
1494 cupsLangFree(language
);
1505 * 'cups_queue_resume()' - Restart a print queue.
1508 static int cups_queue_resume(int snum
)
1510 TALLOC_CTX
*frame
= talloc_stackframe();
1511 int ret
= 1; /* Return value */
1512 http_t
*http
= NULL
; /* HTTP connection to server */
1513 ipp_t
*request
= NULL
, /* IPP Request */
1514 *response
= NULL
; /* IPP Response */
1515 cups_lang_t
*language
= NULL
; /* Default language */
1516 char *printername
= NULL
;
1517 char *username
= NULL
;
1518 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
1521 DEBUG(5,("cups_queue_resume(%d)\n", snum
));
1524 * Make sure we don't ask for passwords...
1527 cupsSetPasswordCB(cups_passwd_cb
);
1530 * Try to connect to the server...
1533 if ((http
= cups_connect(frame
)) == NULL
) {
1538 * Build an IPP_RESUME_PRINTER request, which requires the following
1541 * attributes-charset
1542 * attributes-natural-language
1544 * requesting-user-name
1549 ippSetOperation(request
, IPP_RESUME_PRINTER
);
1550 ippSetRequestId(request
, 1);
1552 language
= cupsLangDefault();
1554 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1555 "attributes-charset", NULL
, "utf-8");
1557 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1558 "attributes-natural-language", NULL
, language
->language
);
1560 if (!push_utf8_talloc(frame
, &printername
, lp_printername(snum
),
1564 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/printers/%s",
1567 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
1569 if (!push_utf8_talloc(frame
, &username
, current_user_info
.unix_name
, &size
)) {
1572 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
1576 * Do the request and get back a response...
1579 if ((response
= cupsDoRequest(http
, request
, "/admin/")) != NULL
) {
1580 if (ippGetStatusCode(response
) >= IPP_OK_CONFLICT
) {
1581 DEBUG(0,("Unable to resume printer %s - %s\n",
1582 lp_printername(snum
),
1583 ippErrorString(cupsLastError())));
1588 DEBUG(0,("Unable to resume printer %s - %s\n",
1589 lp_printername(snum
),
1590 ippErrorString(cupsLastError())));
1595 ippDelete(response
);
1598 cupsLangFree(language
);
1607 /*******************************************************************
1608 * CUPS printing interface definitions...
1609 ******************************************************************/
1611 struct printif cups_printif
=
1624 /* this keeps fussy compilers happy */
1625 void print_cups_dummy(void);
1626 void print_cups_dummy(void) {}
1627 #endif /* HAVE_CUPS */