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]);
486 TALLOC_FREE(msg_ctx
);
490 struct cups_async_cb_args
{
492 struct tevent_context
*event_ctx
;
493 struct messaging_context
*msg_ctx
;
494 void (*post_cache_fill_fn
)(struct tevent_context
*,
495 struct messaging_context
*);
498 static void cups_async_callback(struct tevent_context
*event_ctx
,
499 struct tevent_fd
*event
,
503 TALLOC_CTX
*frame
= talloc_stackframe();
504 struct cups_async_cb_args
*cb_args
= (struct cups_async_cb_args
*)p
;
505 struct pcap_cache
*tmp_pcap_cache
= NULL
;
507 struct pcap_data pcap_data
;
509 enum ndr_err_code ndr_ret
;
512 DEBUG(5,("cups_async_callback: callback received for printer data. "
513 "fd = %d\n", cb_args
->pipe_fd
));
515 ret_ok
= recv_pcap_blob(frame
, cb_args
->pipe_fd
, &pcap_blob
);
517 DEBUG(0,("failed to recv pcap blob\n"));
521 ndr_ret
= ndr_pull_struct_blob(&pcap_blob
, frame
, &pcap_data
,
522 (ndr_pull_flags_fn_t
)ndr_pull_pcap_data
);
523 if (ndr_ret
!= NDR_ERR_SUCCESS
) {
527 if (!NT_STATUS_IS_OK(pcap_data
.status
)) {
528 DEBUG(0,("failed to retrieve printer list: %s\n",
529 nt_errstr(pcap_data
.status
)));
533 for (i
= 0; i
< pcap_data
.count
; i
++) {
534 ret_ok
= pcap_cache_add_specific(&tmp_pcap_cache
,
535 pcap_data
.printers
[i
].name
,
536 pcap_data
.printers
[i
].info
,
537 pcap_data
.printers
[i
].location
);
539 DEBUG(0, ("failed to add to tmp pcap cache\n"));
544 /* replace the system-wide pcap cache with a (possibly empty) new one */
545 ret_ok
= pcap_cache_replace(tmp_pcap_cache
);
547 DEBUG(0, ("failed to replace pcap cache\n"));
548 } else if (cb_args
->post_cache_fill_fn
!= NULL
) {
549 /* Caller requested post cache fill callback */
550 cb_args
->post_cache_fill_fn(cb_args
->event_ctx
,
554 pcap_cache_destroy_specific(&tmp_pcap_cache
);
556 close(cb_args
->pipe_fd
);
557 TALLOC_FREE(cb_args
);
558 TALLOC_FREE(cache_fd_event
);
561 bool cups_cache_reload(struct tevent_context
*ev
,
562 struct messaging_context
*msg_ctx
,
563 void (*post_cache_fill_fn
)(struct tevent_context
*,
564 struct messaging_context
*))
566 struct cups_async_cb_args
*cb_args
;
569 cb_args
= talloc(NULL
, struct cups_async_cb_args
);
570 if (cb_args
== NULL
) {
574 cb_args
->post_cache_fill_fn
= post_cache_fill_fn
;
575 cb_args
->event_ctx
= ev
;
576 cb_args
->msg_ctx
= msg_ctx
;
577 p_pipe_fd
= &cb_args
->pipe_fd
;
580 /* Set up an async refresh. */
581 if (!cups_pcap_load_async(ev
, msg_ctx
, p_pipe_fd
)) {
582 talloc_free(cb_args
);
586 DEBUG(10,("cups_cache_reload: async read on fd %d\n",
589 /* Trigger an event when the pipe can be read. */
590 cache_fd_event
= tevent_add_fd(ev
,
595 if (!cache_fd_event
) {
597 TALLOC_FREE(cb_args
);
605 * 'cups_job_delete()' - Delete a job.
608 static int cups_job_delete(const char *sharename
, const char *lprm_command
, struct printjob
*pjob
)
610 TALLOC_CTX
*frame
= talloc_stackframe();
611 int ret
= 1; /* Return value */
612 http_t
*http
= NULL
; /* HTTP connection to server */
613 ipp_t
*request
= NULL
, /* IPP Request */
614 *response
= NULL
; /* IPP Response */
615 cups_lang_t
*language
= NULL
; /* Default language */
617 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
620 DEBUG(5,("cups_job_delete(%s, %p (%d))\n", sharename
, pjob
, pjob
->sysjob
));
623 * Make sure we don't ask for passwords...
626 cupsSetPasswordCB(cups_passwd_cb
);
629 * Try to connect to the server...
632 if ((http
= cups_connect(frame
)) == NULL
) {
637 * Build an IPP_CANCEL_JOB request, which requires the following
641 * attributes-natural-language
643 * requesting-user-name
648 ippSetOperation(request
, IPP_CANCEL_JOB
);
649 ippSetRequestId(request
, 1);
651 language
= cupsLangDefault();
653 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
654 "attributes-charset", NULL
, "utf-8");
656 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
657 "attributes-natural-language", NULL
, language
->language
);
659 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/jobs/%d", pjob
->sysjob
);
661 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
663 if (!push_utf8_talloc(frame
, &user
, pjob
->user
, &size
)) {
667 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
671 * Do the request and get back a response...
674 if ((response
= cupsDoRequest(http
, request
, "/jobs")) != NULL
) {
675 if (ippGetStatusCode(response
) >= IPP_OK_CONFLICT
) {
676 DEBUG(0,("Unable to cancel job %d - %s\n", pjob
->sysjob
,
677 ippErrorString(cupsLastError())));
682 DEBUG(0,("Unable to cancel job %d - %s\n", pjob
->sysjob
,
683 ippErrorString(cupsLastError())));
691 cupsLangFree(language
);
702 * 'cups_job_pause()' - Pause a job.
705 static int cups_job_pause(int snum
, struct printjob
*pjob
)
707 TALLOC_CTX
*frame
= talloc_stackframe();
708 int ret
= 1; /* Return value */
709 http_t
*http
= NULL
; /* HTTP connection to server */
710 ipp_t
*request
= NULL
, /* IPP Request */
711 *response
= NULL
; /* IPP Response */
712 cups_lang_t
*language
= NULL
; /* Default language */
714 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
717 DEBUG(5,("cups_job_pause(%d, %p (%d))\n", snum
, pjob
, pjob
->sysjob
));
720 * Make sure we don't ask for passwords...
723 cupsSetPasswordCB(cups_passwd_cb
);
726 * Try to connect to the server...
729 if ((http
= cups_connect(frame
)) == NULL
) {
734 * Build an IPP_HOLD_JOB request, which requires the following
738 * attributes-natural-language
740 * requesting-user-name
745 ippSetOperation(request
, IPP_HOLD_JOB
);
746 ippSetRequestId(request
, 1);
748 language
= cupsLangDefault();
750 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
751 "attributes-charset", NULL
, "utf-8");
753 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
754 "attributes-natural-language", NULL
, language
->language
);
756 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/jobs/%d", pjob
->sysjob
);
758 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
760 if (!push_utf8_talloc(frame
, &user
, pjob
->user
, &size
)) {
763 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
767 * Do the request and get back a response...
770 if ((response
= cupsDoRequest(http
, request
, "/jobs")) != NULL
) {
771 if (ippGetStatusCode(response
) >= IPP_OK_CONFLICT
) {
772 DEBUG(0,("Unable to hold job %d - %s\n", pjob
->sysjob
,
773 ippErrorString(cupsLastError())));
778 DEBUG(0,("Unable to hold job %d - %s\n", pjob
->sysjob
,
779 ippErrorString(cupsLastError())));
787 cupsLangFree(language
);
798 * 'cups_job_resume()' - Resume a paused job.
801 static int cups_job_resume(int snum
, struct printjob
*pjob
)
803 TALLOC_CTX
*frame
= talloc_stackframe();
804 int ret
= 1; /* Return value */
805 http_t
*http
= NULL
; /* HTTP connection to server */
806 ipp_t
*request
= NULL
, /* IPP Request */
807 *response
= NULL
; /* IPP Response */
808 cups_lang_t
*language
= NULL
; /* Default language */
810 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
813 DEBUG(5,("cups_job_resume(%d, %p (%d))\n", snum
, pjob
, pjob
->sysjob
));
816 * Make sure we don't ask for passwords...
819 cupsSetPasswordCB(cups_passwd_cb
);
822 * Try to connect to the server...
825 if ((http
= cups_connect(frame
)) == NULL
) {
830 * Build an IPP_RELEASE_JOB request, which requires the following
834 * attributes-natural-language
836 * requesting-user-name
841 ippSetOperation(request
, IPP_RELEASE_JOB
);
842 ippSetRequestId(request
, 1);
844 language
= cupsLangDefault();
846 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
847 "attributes-charset", NULL
, "utf-8");
849 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
850 "attributes-natural-language", NULL
, language
->language
);
852 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/jobs/%d", pjob
->sysjob
);
854 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
856 if (!push_utf8_talloc(frame
, &user
, pjob
->user
, &size
)) {
859 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
863 * Do the request and get back a response...
866 if ((response
= cupsDoRequest(http
, request
, "/jobs")) != NULL
) {
867 if (ippGetStatusCode(response
) >= IPP_OK_CONFLICT
) {
868 DEBUG(0,("Unable to release job %d - %s\n", pjob
->sysjob
,
869 ippErrorString(cupsLastError())));
874 DEBUG(0,("Unable to release job %d - %s\n", pjob
->sysjob
,
875 ippErrorString(cupsLastError())));
883 cupsLangFree(language
);
894 * 'cups_job_submit()' - Submit a job for printing.
897 static int cups_job_submit(int snum
, struct printjob
*pjob
,
898 enum printing_types printing_type
,
901 TALLOC_CTX
*frame
= talloc_stackframe();
902 int ret
= 1; /* Return value */
903 http_t
*http
= NULL
; /* HTTP connection to server */
904 ipp_t
*request
= NULL
, /* IPP Request */
905 *response
= NULL
; /* IPP Response */
906 ipp_attribute_t
*attr_job_id
= NULL
; /* IPP Attribute "job-id" */
907 cups_lang_t
*language
= NULL
; /* Default language */
908 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
909 char *new_jobname
= NULL
;
911 cups_option_t
*options
= NULL
;
912 char *printername
= NULL
;
914 char *jobname
= NULL
;
915 char *cupsoptions
= NULL
;
916 char *filename
= NULL
;
919 DEBUG(5,("cups_job_submit(%d, %p)\n", snum
, pjob
));
922 * Make sure we don't ask for passwords...
925 cupsSetPasswordCB(cups_passwd_cb
);
928 * Try to connect to the server...
931 if ((http
= cups_connect(frame
)) == NULL
) {
936 * Build an IPP_PRINT_JOB request, which requires the following
940 * attributes-natural-language
942 * requesting-user-name
948 ippSetOperation(request
, IPP_PRINT_JOB
);
949 ippSetRequestId(request
, 1);
951 language
= cupsLangDefault();
953 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
954 "attributes-charset", NULL
, "utf-8");
956 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
957 "attributes-natural-language", NULL
, language
->language
);
959 if (!push_utf8_talloc(frame
, &printername
,
960 lp_printername(talloc_tos(), snum
),
964 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/printers/%s",
967 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
968 "printer-uri", NULL
, uri
);
970 if (!push_utf8_talloc(frame
, &user
, pjob
->user
, &size
)) {
973 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
976 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
977 "job-originating-host-name", NULL
,
978 pjob
->clientmachine
);
980 if (!push_utf8_talloc(frame
, &jobname
, pjob
->jobname
, &size
)) {
983 new_jobname
= talloc_asprintf(frame
,
984 "%s%.8u %s", PRINT_SPOOL_PREFIX
,
985 pjob
->jobid
, jobname
);
986 if (new_jobname
== NULL
) {
990 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name", NULL
,
994 * add any options defined in smb.conf
997 if (!push_utf8_talloc(frame
, &cupsoptions
,
998 lp_cups_options(talloc_tos(), snum
), &size
)) {
1003 num_options
= cupsParseOptions(cupsoptions
, num_options
, &options
);
1006 cupsEncodeOptions(request
, num_options
, options
);
1009 * Do the request and get back a response...
1012 slprintf(uri
, sizeof(uri
) - 1, "/printers/%s", printername
);
1014 if (!push_utf8_talloc(frame
, &filename
, pjob
->filename
, &size
)) {
1017 if ((response
= cupsDoFileRequest(http
, request
, uri
, pjob
->filename
)) != NULL
) {
1018 if (ippGetStatusCode(response
) >= IPP_OK_CONFLICT
) {
1019 DEBUG(0,("Unable to print file to %s - %s\n",
1020 lp_printername(talloc_tos(), snum
),
1021 ippErrorString(cupsLastError())));
1024 attr_job_id
= ippFindAttribute(response
, "job-id", IPP_TAG_INTEGER
);
1026 pjob
->sysjob
= ippGetInteger(attr_job_id
, 0);
1027 DEBUG(5,("cups_job_submit: job-id %d\n", pjob
->sysjob
));
1029 DEBUG(0,("Missing job-id attribute in IPP response"));
1033 DEBUG(0,("Unable to print file to `%s' - %s\n",
1034 lp_printername(talloc_tos(), snum
),
1035 ippErrorString(cupsLastError())));
1039 unlink(pjob
->filename
);
1040 /* else print_job_end will do it for us */
1044 ippDelete(response
);
1047 cupsLangFree(language
);
1058 * 'cups_queue_get()' - Get all the jobs in the print queue.
1061 static int cups_queue_get(const char *sharename
,
1062 enum printing_types printing_type
,
1064 print_queue_struct
**q
,
1065 print_status_struct
*status
)
1067 TALLOC_CTX
*frame
= talloc_stackframe();
1068 char *printername
= NULL
;
1069 http_t
*http
= NULL
; /* HTTP connection to server */
1070 ipp_t
*request
= NULL
, /* IPP Request */
1071 *response
= NULL
; /* IPP Response */
1072 ipp_attribute_t
*attr
= NULL
; /* Current attribute */
1073 cups_lang_t
*language
= NULL
; /* Default language */
1074 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
1075 int qcount
= 0, /* Number of active queue entries */
1076 qalloc
= 0; /* Number of queue entries allocated */
1077 print_queue_struct
*queue
= NULL
, /* Queue entries */
1078 *temp
; /* Temporary pointer for queue */
1079 char *user_name
= NULL
, /* job-originating-user-name attribute */
1080 *job_name
= NULL
; /* job-name attribute */
1081 int job_id
; /* job-id attribute */
1082 int job_k_octets
; /* job-k-octets attribute */
1083 time_t job_time
; /* time-at-creation attribute */
1084 ipp_jstate_t job_status
; /* job-status attribute */
1085 int job_priority
; /* job-priority attribute */
1087 static const char *jattrs
[] = /* Requested job attributes */
1092 "job-originating-user-name",
1097 static const char *pattrs
[] = /* Requested printer attributes */
1100 "printer-state-message"
1105 /* HACK ALERT!!! The problem with support the 'printer name'
1106 option is that we key the tdb off the sharename. So we will
1107 overload the lpq_command string to pass in the printername
1108 (which is basically what we do for non-cups printers ... using
1109 the lpq_command to get the queue listing). */
1111 if (!push_utf8_talloc(frame
, &printername
, lpq_command
, &size
)) {
1114 DEBUG(5,("cups_queue_get(%s, %p, %p)\n", lpq_command
, q
, status
));
1117 * Make sure we don't ask for passwords...
1120 cupsSetPasswordCB(cups_passwd_cb
);
1123 * Try to connect to the server...
1126 if ((http
= cups_connect(frame
)) == NULL
) {
1131 * Generate the printer URI...
1134 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/printers/%s", printername
);
1137 * Build an IPP_GET_JOBS request, which requires the following
1140 * attributes-charset
1141 * attributes-natural-language
1142 * requested-attributes
1148 ippSetOperation(request
, IPP_GET_JOBS
);
1149 ippSetRequestId(request
, 1);
1151 language
= cupsLangDefault();
1153 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1154 "attributes-charset", NULL
, "utf-8");
1156 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1157 "attributes-natural-language", NULL
, language
->language
);
1159 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1160 "requested-attributes",
1161 (sizeof(jattrs
) / sizeof(jattrs
[0])),
1164 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1165 "printer-uri", NULL
, uri
);
1168 * Do the request and get back a response...
1171 if ((response
= cupsDoRequest(http
, request
, "/")) == NULL
) {
1172 DEBUG(0,("Unable to get jobs for %s - %s\n", uri
,
1173 ippErrorString(cupsLastError())));
1177 if (ippGetStatusCode(response
) >= IPP_OK_CONFLICT
) {
1178 DEBUG(0,("Unable to get jobs for %s - %s\n", uri
,
1179 ippErrorString(ippGetStatusCode(response
))));
1184 * Process the jobs...
1191 for (attr
= ippFirstAttribute(response
); attr
!= NULL
; attr
= ippNextAttribute(response
)) {
1193 * Skip leading attributes until we hit a job...
1196 while (attr
!= NULL
&& ippGetGroupTag(attr
) != IPP_TAG_JOB
)
1197 attr
= ippNextAttribute(response
);
1203 * Allocate memory as needed...
1205 if (qcount
>= qalloc
) {
1208 queue
= SMB_REALLOC_ARRAY(queue
, print_queue_struct
, qalloc
);
1210 if (queue
== NULL
) {
1211 DEBUG(0,("cups_queue_get: Not enough memory!"));
1217 temp
= queue
+ qcount
;
1218 memset(temp
, 0, sizeof(print_queue_struct
));
1221 * Pull the needed attributes from this job...
1226 job_status
= IPP_JOB_PENDING
;
1232 while (attr
!= NULL
&& ippGetGroupTag(attr
) == IPP_TAG_JOB
) {
1233 if (ippGetName(attr
) == NULL
) {
1234 attr
= ippNextAttribute(response
);
1238 if (strcmp(ippGetName(attr
), "job-id") == 0 &&
1239 ippGetValueTag(attr
) == IPP_TAG_INTEGER
)
1240 job_id
= ippGetInteger(attr
, 0);
1242 if (strcmp(ippGetName(attr
), "job-k-octets") == 0 &&
1243 ippGetValueTag(attr
) == IPP_TAG_INTEGER
)
1244 job_k_octets
= ippGetInteger(attr
, 0);
1246 if (strcmp(ippGetName(attr
), "job-priority") == 0 &&
1247 ippGetValueTag(attr
) == IPP_TAG_INTEGER
)
1248 job_priority
= ippGetInteger(attr
, 0);
1250 if (strcmp(ippGetName(attr
), "job-state") == 0 &&
1251 ippGetValueTag(attr
) == IPP_TAG_ENUM
)
1252 job_status
= (ipp_jstate_t
)ippGetInteger(attr
, 0);
1254 if (strcmp(ippGetName(attr
), "time-at-creation") == 0 &&
1255 ippGetValueTag(attr
) == IPP_TAG_INTEGER
)
1256 job_time
= ippGetInteger(attr
, 0);
1258 if (strcmp(ippGetName(attr
), "job-name") == 0 &&
1259 ippGetValueTag(attr
) == IPP_TAG_NAME
) {
1260 if (!pull_utf8_talloc(frame
,
1262 ippGetString(attr
, 0, NULL
),
1268 if (strcmp(ippGetName(attr
), "job-originating-user-name") == 0 &&
1269 ippGetValueTag(attr
) == IPP_TAG_NAME
) {
1270 if (!pull_utf8_talloc(frame
,
1272 ippGetString(attr
, 0, NULL
),
1278 attr
= ippNextAttribute(response
);
1282 * See if we have everything needed...
1285 if (user_name
== NULL
|| job_name
== NULL
|| job_id
== 0) {
1292 temp
->sysjob
= job_id
;
1293 temp
->size
= job_k_octets
* 1024;
1294 temp
->status
= job_status
== IPP_JOB_PENDING
? LPQ_QUEUED
:
1295 job_status
== IPP_JOB_STOPPED
? LPQ_PAUSED
:
1296 job_status
== IPP_JOB_HELD
? LPQ_PAUSED
:
1298 temp
->priority
= job_priority
;
1299 temp
->time
= job_time
;
1300 strlcpy(temp
->fs_user
, user_name
, sizeof(temp
->fs_user
));
1301 strlcpy(temp
->fs_file
, job_name
, sizeof(temp
->fs_file
));
1309 ippDelete(response
);
1313 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the
1314 * following attributes:
1316 * attributes-charset
1317 * attributes-natural-language
1318 * requested-attributes
1324 ippSetOperation(request
, IPP_GET_PRINTER_ATTRIBUTES
);
1325 ippSetRequestId(request
, 1);
1327 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1328 "attributes-charset", NULL
, "utf-8");
1330 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1331 "attributes-natural-language", NULL
, language
->language
);
1333 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1334 "requested-attributes",
1335 (sizeof(pattrs
) / sizeof(pattrs
[0])),
1338 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1339 "printer-uri", NULL
, uri
);
1342 * Do the request and get back a response...
1345 if ((response
= cupsDoRequest(http
, request
, "/")) == NULL
) {
1346 DEBUG(0,("Unable to get printer status for %s - %s\n", printername
,
1347 ippErrorString(cupsLastError())));
1351 if (ippGetStatusCode(response
) >= IPP_OK_CONFLICT
) {
1352 DEBUG(0,("Unable to get printer status for %s - %s\n", printername
,
1353 ippErrorString(ippGetStatusCode(response
))));
1358 * Get the current printer status and convert it to the SAMBA values.
1361 if ((attr
= ippFindAttribute(response
, "printer-state", IPP_TAG_ENUM
)) != NULL
) {
1362 if (ippGetInteger(attr
, 0) == IPP_PRINTER_STOPPED
)
1363 status
->status
= LPSTAT_STOPPED
;
1365 status
->status
= LPSTAT_OK
;
1368 if ((attr
= ippFindAttribute(response
, "printer-state-message",
1369 IPP_TAG_TEXT
)) != NULL
) {
1371 if (!pull_utf8_talloc(frame
, &msg
,
1372 ippGetString(attr
, 0, NULL
),
1378 fstrcpy(status
->message
, msg
);
1384 * Return the job queue...
1390 ippDelete(response
);
1393 cupsLangFree(language
);
1404 * 'cups_queue_pause()' - Pause a print queue.
1407 static int cups_queue_pause(int snum
)
1409 TALLOC_CTX
*frame
= talloc_stackframe();
1410 int ret
= 1; /* Return value */
1411 http_t
*http
= NULL
; /* HTTP connection to server */
1412 ipp_t
*request
= NULL
, /* IPP Request */
1413 *response
= NULL
; /* IPP Response */
1414 cups_lang_t
*language
= NULL
; /* Default language */
1415 char *printername
= NULL
;
1416 char *username
= NULL
;
1417 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
1420 DEBUG(5,("cups_queue_pause(%d)\n", snum
));
1423 * Make sure we don't ask for passwords...
1426 cupsSetPasswordCB(cups_passwd_cb
);
1429 * Try to connect to the server...
1432 if ((http
= cups_connect(frame
)) == NULL
) {
1437 * Build an IPP_PAUSE_PRINTER request, which requires the following
1440 * attributes-charset
1441 * attributes-natural-language
1443 * requesting-user-name
1448 ippSetOperation(request
, IPP_PAUSE_PRINTER
);
1449 ippSetRequestId(request
, 1);
1451 language
= cupsLangDefault();
1453 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1454 "attributes-charset", NULL
, "utf-8");
1456 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1457 "attributes-natural-language", NULL
, language
->language
);
1459 if (!push_utf8_talloc(frame
, &printername
,
1460 lp_printername(talloc_tos(), snum
), &size
)) {
1463 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/printers/%s",
1466 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
1468 if (!push_utf8_talloc(frame
, &username
, current_user_info
.unix_name
, &size
)) {
1471 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
1475 * Do the request and get back a response...
1478 if ((response
= cupsDoRequest(http
, request
, "/admin/")) != NULL
) {
1479 if (ippGetStatusCode(response
) >= IPP_OK_CONFLICT
) {
1480 DEBUG(0,("Unable to pause printer %s - %s\n",
1481 lp_printername(talloc_tos(), snum
),
1482 ippErrorString(cupsLastError())));
1487 DEBUG(0,("Unable to pause printer %s - %s\n",
1488 lp_printername(talloc_tos(), snum
),
1489 ippErrorString(cupsLastError())));
1494 ippDelete(response
);
1497 cupsLangFree(language
);
1508 * 'cups_queue_resume()' - Restart a print queue.
1511 static int cups_queue_resume(int snum
)
1513 TALLOC_CTX
*frame
= talloc_stackframe();
1514 int ret
= 1; /* Return value */
1515 http_t
*http
= NULL
; /* HTTP connection to server */
1516 ipp_t
*request
= NULL
, /* IPP Request */
1517 *response
= NULL
; /* IPP Response */
1518 cups_lang_t
*language
= NULL
; /* Default language */
1519 char *printername
= NULL
;
1520 char *username
= NULL
;
1521 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
1524 DEBUG(5,("cups_queue_resume(%d)\n", snum
));
1527 * Make sure we don't ask for passwords...
1530 cupsSetPasswordCB(cups_passwd_cb
);
1533 * Try to connect to the server...
1536 if ((http
= cups_connect(frame
)) == NULL
) {
1541 * Build an IPP_RESUME_PRINTER request, which requires the following
1544 * attributes-charset
1545 * attributes-natural-language
1547 * requesting-user-name
1552 ippSetOperation(request
, IPP_RESUME_PRINTER
);
1553 ippSetRequestId(request
, 1);
1555 language
= cupsLangDefault();
1557 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1558 "attributes-charset", NULL
, "utf-8");
1560 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1561 "attributes-natural-language", NULL
, language
->language
);
1563 if (!push_utf8_talloc(frame
, &printername
, lp_printername(talloc_tos(), snum
),
1567 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/printers/%s",
1570 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
1572 if (!push_utf8_talloc(frame
, &username
, current_user_info
.unix_name
, &size
)) {
1575 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
1579 * Do the request and get back a response...
1582 if ((response
= cupsDoRequest(http
, request
, "/admin/")) != NULL
) {
1583 if (ippGetStatusCode(response
) >= IPP_OK_CONFLICT
) {
1584 DEBUG(0,("Unable to resume printer %s - %s\n",
1585 lp_printername(talloc_tos(), snum
),
1586 ippErrorString(cupsLastError())));
1591 DEBUG(0,("Unable to resume printer %s - %s\n",
1592 lp_printername(talloc_tos(), snum
),
1593 ippErrorString(cupsLastError())));
1598 ippDelete(response
);
1601 cupsLangFree(language
);
1610 /*******************************************************************
1611 * CUPS printing interface definitions...
1612 ******************************************************************/
1614 struct printif cups_printif
=
1627 /* this keeps fussy compilers happy */
1628 void print_cups_dummy(void);
1629 void print_cups_dummy(void) {}
1630 #endif /* HAVE_CUPS */