2 * Support code for the Common UNIX Printing System ("CUPS")
4 * Copyright 1999-2003 by Michael R Sweet.
5 * Copyright 2008 Jeremy Allison.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
22 * JRA. Converted to utf8 pull/push.
27 #include "printing/pcap.h"
28 #include "librpc/gen_ndr/ndr_printcap.h"
29 #include "lib/util/sys_rw.h"
30 #include "lib/util/string_wrappers.h"
33 #include <cups/cups.h>
34 #include <cups/language.h>
35 #include <cups/http.h>
37 /* CUPS prior to version 1.7 doesn't have HTTP_URI_STATUS_OK */
38 #if (CUPS_VERSION_MAJOR == 1) && (CUPS_VERSION_MINOR < 7)
39 #define HTTP_URI_STATUS_OK HTTP_URI_OK
42 #if (CUPS_VERSION_MAJOR > 1) || (CUPS_VERSION_MINOR > 5)
43 #define HAVE_CUPS_1_6 1
47 #define ippGetGroupTag(attr) attr->group_tag
48 #define ippGetName(attr) attr->name
49 #define ippGetValueTag(attr) attr->value_tag
50 #define ippGetStatusCode(ipp) ipp->request.status.status_code
51 #define ippGetInteger(attr, element) attr->values[element].integer
52 #define ippGetString(attr, element, language) attr->values[element].string.text
54 static ipp_attribute_t
*
55 ippFirstAttribute(ipp_t
*ipp
)
59 return (ipp
->current
= ipp
->attrs
);
62 static ipp_attribute_t
*
63 ippNextAttribute(ipp_t
*ipp
)
65 if (!ipp
|| !ipp
->current
)
67 return (ipp
->current
= ipp
->current
->next
);
70 static int ippSetOperation(ipp_t
*ipp
, ipp_op_t op
)
72 ipp
->request
.op
.operation_id
= op
;
76 static int ippSetRequestId(ipp_t
*ipp
, int request_id
)
78 ipp
->request
.any
.request_id
= request_id
;
83 static SIG_ATOMIC_T gotalarm
;
85 /***************************************************************
86 Signal function to tell us we timed out.
87 ****************************************************************/
89 static void gotalarm_sig(int signum
)
94 extern userdom_struct current_user_info
;
97 * 'cups_passwd_cb()' - The CUPS password callback...
100 static const char * /* O - Password or NULL */
101 cups_passwd_cb(const char *prompt
) /* I - Prompt */
104 * Always return NULL to indicate that no password is available...
110 static http_t
*cups_connect(TALLOC_CTX
*frame
)
112 const struct loadparm_substitution
*lp_sub
=
113 loadparm_s3_global_substitution();
115 char *server
= NULL
, *p
= NULL
;
117 int timeout
= lp_cups_connection_timeout();
120 if (lp_cups_server(talloc_tos(), lp_sub
) != NULL
&& strlen(lp_cups_server(talloc_tos(), lp_sub
)) > 0) {
121 if (!push_utf8_talloc(frame
, &server
, lp_cups_server(talloc_tos(), lp_sub
), &size
)) {
125 server
= talloc_strdup(frame
,cupsServer());
131 p
= strchr(server
, ':');
139 DEBUG(10, ("connecting to cups server %s:%d\n",
145 CatchSignal(SIGALRM
, gotalarm_sig
);
149 #if defined(HAVE_HTTPCONNECT2)
150 http
= httpConnect2(server
,
155 HTTP_ENCRYPTION_ALWAYS
:
156 HTTP_ENCRYPTION_IF_REQUESTED
,
158 30 * 1000, /* timeout */
160 #elif defined(HAVE_HTTPCONNECTENCRYPT)
161 http
= httpConnectEncrypt(server
, port
, lp_cups_encrypt());
163 http
= httpConnect(server
, port
);
167 CatchSignal(SIGALRM
, SIG_IGN
);
171 DEBUG(3,("Unable to connect to CUPS server %s:%d - %s\n",
172 server
, port
, strerror(errno
)));
178 static bool send_pcap_blob(DATA_BLOB
*pcap_blob
, int fd
)
182 ret
= sys_write(fd
, &pcap_blob
->length
, sizeof(pcap_blob
->length
));
183 if (ret
!= sizeof(pcap_blob
->length
)) {
187 ret
= sys_write(fd
, pcap_blob
->data
, pcap_blob
->length
);
188 if (ret
!= pcap_blob
->length
) {
192 DEBUG(10, ("successfully sent blob of len %d\n", (int)ret
));
196 static bool recv_pcap_blob(TALLOC_CTX
*mem_ctx
, int fd
, DATA_BLOB
*pcap_blob
)
201 ret
= sys_read(fd
, &blob_len
, sizeof(blob_len
));
202 if (ret
!= sizeof(blob_len
)) {
206 *pcap_blob
= data_blob_talloc_named(mem_ctx
, NULL
, blob_len
,
208 if (pcap_blob
->length
!= blob_len
) {
211 ret
= sys_read(fd
, pcap_blob
->data
, blob_len
);
212 if (ret
!= blob_len
) {
213 talloc_free(pcap_blob
->data
);
217 DEBUG(10, ("successfully recvd blob of len %d\n", (int)ret
));
221 static bool process_cups_printers_response(TALLOC_CTX
*mem_ctx
,
223 struct pcap_data
*pcap_data
)
225 ipp_attribute_t
*attr
;
228 char *location
= NULL
;
229 struct pcap_printer
*printer
;
232 for (attr
= ippFirstAttribute(response
); attr
!= NULL
;) {
234 * Skip leading attributes until we hit a printer...
237 while (attr
!= NULL
&& ippGetGroupTag(attr
) != IPP_TAG_PRINTER
)
238 attr
= ippNextAttribute(response
);
244 * Pull the needed attributes from this printer...
250 while (attr
!= NULL
&& ippGetGroupTag(attr
) == IPP_TAG_PRINTER
) {
252 if (strcmp(ippGetName(attr
), "printer-name") == 0 &&
253 ippGetValueTag(attr
) == IPP_TAG_NAME
) {
254 if (!pull_utf8_talloc(mem_ctx
,
256 ippGetString(attr
, 0, NULL
),
262 if (strcmp(ippGetName(attr
), "printer-info") == 0 &&
263 ippGetValueTag(attr
) == IPP_TAG_TEXT
) {
264 if (!pull_utf8_talloc(mem_ctx
,
266 ippGetString(attr
, 0, NULL
),
272 if (strcmp(ippGetName(attr
), "printer-location") == 0 &&
273 ippGetValueTag(attr
) == IPP_TAG_TEXT
) {
274 if (!pull_utf8_talloc(mem_ctx
,
276 ippGetString(attr
, 0, NULL
),
282 attr
= ippNextAttribute(response
);
286 * See if we have everything needed...
292 if (pcap_data
->count
== 0) {
293 printer
= talloc_array(mem_ctx
, struct pcap_printer
, 1);
295 printer
= talloc_realloc(mem_ctx
, pcap_data
->printers
,
297 pcap_data
->count
+ 1);
299 if (printer
== NULL
) {
302 pcap_data
->printers
= printer
;
303 pcap_data
->printers
[pcap_data
->count
].name
= name
;
304 pcap_data
->printers
[pcap_data
->count
].info
= info
;
305 pcap_data
->printers
[pcap_data
->count
].location
= location
;
315 * request printer list from cups, send result back to up parent via fd.
316 * returns true if the (possibly failed) result was successfully sent to parent.
318 static bool cups_cache_reload_async(int fd
)
320 TALLOC_CTX
*frame
= talloc_stackframe();
321 struct pcap_data pcap_data
;
322 http_t
*http
= NULL
; /* HTTP connection to server */
323 ipp_t
*request
= NULL
, /* IPP Request */
324 *response
= NULL
; /* IPP Response */
325 cups_lang_t
*language
= NULL
; /* Default language */
326 static const char *requested
[] =/* Requested attributes */
333 enum ndr_err_code ndr_ret
;
336 ZERO_STRUCT(pcap_data
);
337 pcap_data
.status
= NT_STATUS_UNSUCCESSFUL
;
339 DEBUG(5, ("reloading cups printcap cache\n"));
342 * Make sure we don't ask for passwords...
345 cupsSetPasswordCB(cups_passwd_cb
);
347 if ((http
= cups_connect(frame
)) == NULL
) {
352 * Build a CUPS_GET_PRINTERS request, which requires the following
356 * attributes-natural-language
357 * requested-attributes
362 ippSetOperation(request
, CUPS_GET_PRINTERS
);
363 ippSetRequestId(request
, 1);
365 language
= cupsLangDefault();
367 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
368 "attributes-charset", NULL
, "utf-8");
370 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
371 "attributes-natural-language", NULL
, language
->language
);
373 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
374 "requested-attributes",
375 (sizeof(requested
) / sizeof(requested
[0])),
378 if ((response
= cupsDoRequest(http
, request
, "/")) == NULL
) {
379 DEBUG(0,("Unable to get printer list - %s\n",
380 ippErrorString(cupsLastError())));
384 ret
= process_cups_printers_response(frame
, response
, &pcap_data
);
386 DEBUG(0,("failed to process cups response\n"));
394 * Build a CUPS_GET_CLASSES request, which requires the following
398 * attributes-natural-language
399 * requested-attributes
404 ippSetOperation(request
, CUPS_GET_CLASSES
);
405 ippSetRequestId(request
, 1);
407 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
408 "attributes-charset", NULL
, "utf-8");
410 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
411 "attributes-natural-language", NULL
, language
->language
);
413 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
414 "requested-attributes",
415 (sizeof(requested
) / sizeof(requested
[0])),
418 if ((response
= cupsDoRequest(http
, request
, "/")) == NULL
) {
419 DEBUG(0,("Unable to get printer list - %s\n",
420 ippErrorString(cupsLastError())));
424 ret
= process_cups_printers_response(frame
, response
, &pcap_data
);
426 DEBUG(0,("failed to process cups response\n"));
430 pcap_data
.status
= NT_STATUS_OK
;
436 cupsLangFree(language
);
442 ndr_ret
= ndr_push_struct_blob(&pcap_blob
, frame
, &pcap_data
,
443 (ndr_push_flags_fn_t
)ndr_push_pcap_data
);
444 if (ndr_ret
== NDR_ERR_SUCCESS
) {
445 ret
= send_pcap_blob(&pcap_blob
, fd
);
452 static struct tevent_fd
*cache_fd_event
;
454 static bool cups_pcap_load_async(struct tevent_context
*ev
,
455 struct messaging_context
*msg_ctx
,
464 if (cache_fd_event
) {
465 DEBUG(3,("cups_pcap_load_async: already waiting for "
466 "a refresh event\n" ));
470 DEBUG(5,("cups_pcap_load_async: asynchronously loading cups printers\n"));
472 if (pipe(fds
) == -1) {
477 if (pid
== (pid_t
)-1) {
478 DEBUG(10,("cups_pcap_load_async: fork failed %s\n",
486 DEBUG(10,("cups_pcap_load_async: child pid = %u\n",
487 (unsigned int)pid
));
496 close_all_print_db();
498 status
= reinit_after_fork(msg_ctx
, ev
, true);
499 if (!NT_STATUS_IS_OK(status
)) {
500 DEBUG(0,("cups_pcap_load_async: reinit_after_fork() failed\n"));
501 smb_panic("cups_pcap_load_async: reinit_after_fork() failed");
505 cups_cache_reload_async(fds
[1]);
507 TALLOC_FREE(msg_ctx
);
511 struct cups_async_cb_args
{
513 struct tevent_context
*event_ctx
;
514 struct messaging_context
*msg_ctx
;
515 void (*post_cache_fill_fn
)(struct tevent_context
*,
516 struct messaging_context
*);
519 static void cups_async_callback(struct tevent_context
*event_ctx
,
520 struct tevent_fd
*event
,
524 TALLOC_CTX
*frame
= talloc_stackframe();
525 struct cups_async_cb_args
*cb_args
= (struct cups_async_cb_args
*)p
;
526 struct pcap_cache
*tmp_pcap_cache
= NULL
;
528 struct pcap_data pcap_data
;
530 enum ndr_err_code ndr_ret
;
533 DEBUG(5,("cups_async_callback: callback received for printer data. "
534 "fd = %d\n", cb_args
->pipe_fd
));
536 ret_ok
= recv_pcap_blob(frame
, cb_args
->pipe_fd
, &pcap_blob
);
538 DEBUG(0,("failed to recv pcap blob\n"));
542 ndr_ret
= ndr_pull_struct_blob(&pcap_blob
, frame
, &pcap_data
,
543 (ndr_pull_flags_fn_t
)ndr_pull_pcap_data
);
544 if (ndr_ret
!= NDR_ERR_SUCCESS
) {
548 if (!NT_STATUS_IS_OK(pcap_data
.status
)) {
549 DEBUG(3,("failed to retrieve printer list: %s\n",
550 nt_errstr(pcap_data
.status
)));
554 for (i
= 0; i
< pcap_data
.count
; i
++) {
555 ret_ok
= pcap_cache_add_specific(&tmp_pcap_cache
,
556 pcap_data
.printers
[i
].name
,
557 pcap_data
.printers
[i
].info
,
558 pcap_data
.printers
[i
].location
);
560 DEBUG(0, ("failed to add to tmp pcap cache\n"));
565 /* replace the system-wide pcap cache with a (possibly empty) new one */
566 ret_ok
= pcap_cache_replace(tmp_pcap_cache
);
568 DEBUG(0, ("failed to replace pcap cache\n"));
569 } else if (cb_args
->post_cache_fill_fn
!= NULL
) {
570 /* Caller requested post cache fill callback */
571 cb_args
->post_cache_fill_fn(cb_args
->event_ctx
,
575 pcap_cache_destroy_specific(&tmp_pcap_cache
);
577 TALLOC_FREE(cache_fd_event
);
578 close(cb_args
->pipe_fd
);
579 TALLOC_FREE(cb_args
);
582 bool cups_cache_reload(struct tevent_context
*ev
,
583 struct messaging_context
*msg_ctx
,
584 void (*post_cache_fill_fn
)(struct tevent_context
*,
585 struct messaging_context
*))
587 struct cups_async_cb_args
*cb_args
;
590 cb_args
= talloc(NULL
, struct cups_async_cb_args
);
591 if (cb_args
== NULL
) {
595 cb_args
->post_cache_fill_fn
= post_cache_fill_fn
;
596 cb_args
->event_ctx
= ev
;
597 cb_args
->msg_ctx
= msg_ctx
;
598 p_pipe_fd
= &cb_args
->pipe_fd
;
601 /* Set up an async refresh. */
602 if (!cups_pcap_load_async(ev
, msg_ctx
, p_pipe_fd
)) {
603 talloc_free(cb_args
);
607 DEBUG(10,("cups_cache_reload: async read on fd %d\n",
610 /* Trigger an event when the pipe can be read. */
611 cache_fd_event
= tevent_add_fd(ev
,
616 if (!cache_fd_event
) {
618 TALLOC_FREE(cb_args
);
626 * 'cups_job_delete()' - Delete a job.
629 static int cups_job_delete(const char *sharename
, const char *lprm_command
, struct printjob
*pjob
)
631 TALLOC_CTX
*frame
= talloc_stackframe();
632 int ret
= 1; /* Return value */
633 http_t
*http
= NULL
; /* HTTP connection to server */
634 ipp_t
*request
= NULL
, /* IPP Request */
635 *response
= NULL
; /* IPP Response */
636 cups_lang_t
*language
= NULL
; /* Default language */
638 char uri
[HTTP_MAX_URI
] = {0}; /* printer-uri attribute */
639 http_uri_status_t ustatus
;
642 DEBUG(5,("cups_job_delete(%s, %p (%d))\n", sharename
, pjob
, pjob
->sysjob
));
645 * Make sure we don't ask for passwords...
648 cupsSetPasswordCB(cups_passwd_cb
);
651 * Try to connect to the server...
654 if ((http
= cups_connect(frame
)) == NULL
) {
659 * Build an IPP_CANCEL_JOB request, which requires the following
663 * attributes-natural-language
665 * requesting-user-name
670 ippSetOperation(request
, IPP_CANCEL_JOB
);
671 ippSetRequestId(request
, 1);
673 language
= cupsLangDefault();
675 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
676 "attributes-charset", NULL
, "utf-8");
678 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
679 "attributes-natural-language", NULL
, language
->language
);
681 ustatus
= httpAssembleURIf(HTTP_URI_CODING_ALL
,
690 if (ustatus
!= HTTP_URI_STATUS_OK
) {
694 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
696 if (!push_utf8_talloc(frame
, &user
, pjob
->user
, &size
)) {
700 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
704 * Do the request and get back a response...
707 if ((response
= cupsDoRequest(http
, request
, "/jobs")) != NULL
) {
708 if (ippGetStatusCode(response
) >= IPP_OK_CONFLICT
) {
709 DEBUG(0,("Unable to cancel job %d - %s\n", pjob
->sysjob
,
710 ippErrorString(cupsLastError())));
715 DEBUG(0,("Unable to cancel job %d - %s\n", pjob
->sysjob
,
716 ippErrorString(cupsLastError())));
724 cupsLangFree(language
);
735 * 'cups_job_pause()' - Pause a job.
738 static int cups_job_pause(int snum
, struct printjob
*pjob
)
740 TALLOC_CTX
*frame
= talloc_stackframe();
741 int ret
= 1; /* Return value */
742 http_t
*http
= NULL
; /* HTTP connection to server */
743 ipp_t
*request
= NULL
, /* IPP Request */
744 *response
= NULL
; /* IPP Response */
745 cups_lang_t
*language
= NULL
; /* Default language */
747 char uri
[HTTP_MAX_URI
] = {0}; /* printer-uri attribute */
748 http_uri_status_t ustatus
;
751 DEBUG(5,("cups_job_pause(%d, %p (%d))\n", snum
, pjob
, pjob
->sysjob
));
754 * Make sure we don't ask for passwords...
757 cupsSetPasswordCB(cups_passwd_cb
);
760 * Try to connect to the server...
763 if ((http
= cups_connect(frame
)) == NULL
) {
768 * Build an IPP_HOLD_JOB request, which requires the following
772 * attributes-natural-language
774 * requesting-user-name
779 ippSetOperation(request
, IPP_HOLD_JOB
);
780 ippSetRequestId(request
, 1);
782 language
= cupsLangDefault();
784 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
785 "attributes-charset", NULL
, "utf-8");
787 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
788 "attributes-natural-language", NULL
, language
->language
);
790 ustatus
= httpAssembleURIf(HTTP_URI_CODING_ALL
,
799 if (ustatus
!= HTTP_URI_STATUS_OK
) {
803 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
805 if (!push_utf8_talloc(frame
, &user
, pjob
->user
, &size
)) {
808 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
812 * Do the request and get back a response...
815 if ((response
= cupsDoRequest(http
, request
, "/jobs")) != NULL
) {
816 if (ippGetStatusCode(response
) >= IPP_OK_CONFLICT
) {
817 DEBUG(0,("Unable to hold job %d - %s\n", pjob
->sysjob
,
818 ippErrorString(cupsLastError())));
823 DEBUG(0,("Unable to hold job %d - %s\n", pjob
->sysjob
,
824 ippErrorString(cupsLastError())));
832 cupsLangFree(language
);
843 * 'cups_job_resume()' - Resume a paused job.
846 static int cups_job_resume(int snum
, struct printjob
*pjob
)
848 TALLOC_CTX
*frame
= talloc_stackframe();
849 int ret
= 1; /* Return value */
850 http_t
*http
= NULL
; /* HTTP connection to server */
851 ipp_t
*request
= NULL
, /* IPP Request */
852 *response
= NULL
; /* IPP Response */
853 cups_lang_t
*language
= NULL
; /* Default language */
855 char uri
[HTTP_MAX_URI
] = {0}; /* printer-uri attribute */
856 http_uri_status_t ustatus
;
859 DEBUG(5,("cups_job_resume(%d, %p (%d))\n", snum
, pjob
, pjob
->sysjob
));
862 * Make sure we don't ask for passwords...
865 cupsSetPasswordCB(cups_passwd_cb
);
868 * Try to connect to the server...
871 if ((http
= cups_connect(frame
)) == NULL
) {
876 * Build an IPP_RELEASE_JOB request, which requires the following
880 * attributes-natural-language
882 * requesting-user-name
887 ippSetOperation(request
, IPP_RELEASE_JOB
);
888 ippSetRequestId(request
, 1);
890 language
= cupsLangDefault();
892 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
893 "attributes-charset", NULL
, "utf-8");
895 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
896 "attributes-natural-language", NULL
, language
->language
);
898 ustatus
= httpAssembleURIf(HTTP_URI_CODING_ALL
,
907 if (ustatus
!= HTTP_URI_STATUS_OK
) {
911 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
913 if (!push_utf8_talloc(frame
, &user
, pjob
->user
, &size
)) {
916 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
920 * Do the request and get back a response...
923 if ((response
= cupsDoRequest(http
, request
, "/jobs")) != NULL
) {
924 if (ippGetStatusCode(response
) >= IPP_OK_CONFLICT
) {
925 DEBUG(0,("Unable to release job %d - %s\n", pjob
->sysjob
,
926 ippErrorString(cupsLastError())));
931 DEBUG(0,("Unable to release job %d - %s\n", pjob
->sysjob
,
932 ippErrorString(cupsLastError())));
940 cupsLangFree(language
);
951 * 'cups_job_submit()' - Submit a job for printing.
954 static int cups_job_submit(int snum
, struct printjob
*pjob
,
955 enum printing_types printing_type
,
958 TALLOC_CTX
*frame
= talloc_stackframe();
959 const struct loadparm_substitution
*lp_sub
=
960 loadparm_s3_global_substitution();
961 int ret
= 1; /* Return value */
962 http_t
*http
= NULL
; /* HTTP connection to server */
963 ipp_t
*request
= NULL
, /* IPP Request */
964 *response
= NULL
; /* IPP Response */
965 ipp_attribute_t
*attr_job_id
= NULL
; /* IPP Attribute "job-id" */
966 cups_lang_t
*language
= NULL
; /* Default language */
967 char uri
[HTTP_MAX_URI
] = {0}; /* printer-uri attribute */
968 http_uri_status_t ustatus
;
969 char *new_jobname
= NULL
;
971 cups_option_t
*options
= NULL
;
972 char *printername
= NULL
;
974 char *jobname
= NULL
;
975 char *cupsoptions
= NULL
;
976 char *filename
= NULL
;
979 DEBUG(5,("cups_job_submit(%d, %p)\n", snum
, pjob
));
982 * Make sure we don't ask for passwords...
985 cupsSetPasswordCB(cups_passwd_cb
);
988 * Try to connect to the server...
991 if ((http
= cups_connect(frame
)) == NULL
) {
996 * Build an IPP_PRINT_JOB request, which requires the following
1000 * attributes-natural-language
1002 * requesting-user-name
1008 ippSetOperation(request
, IPP_PRINT_JOB
);
1009 ippSetRequestId(request
, 1);
1011 language
= cupsLangDefault();
1013 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1014 "attributes-charset", NULL
, "utf-8");
1016 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1017 "attributes-natural-language", NULL
, language
->language
);
1019 if (!push_utf8_talloc(frame
, &printername
,
1020 lp_printername(talloc_tos(), lp_sub
, snum
),
1024 ustatus
= httpAssembleURIf(HTTP_URI_CODING_ALL
,
1028 NULL
, /* username */
1033 if (ustatus
!= HTTP_URI_STATUS_OK
) {
1037 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1038 "printer-uri", NULL
, uri
);
1040 if (!push_utf8_talloc(frame
, &user
, pjob
->user
, &size
)) {
1043 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
1046 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1047 "job-originating-host-name", NULL
,
1048 pjob
->clientmachine
);
1050 if (!push_utf8_talloc(frame
, &jobname
, pjob
->jobname
, &size
)) {
1053 new_jobname
= talloc_asprintf(frame
,
1054 "%s%.8u %s", PRINT_SPOOL_PREFIX
,
1055 pjob
->jobid
, jobname
);
1056 if (new_jobname
== NULL
) {
1060 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name", NULL
,
1064 * add any options defined in smb.conf
1067 if (!push_utf8_talloc(frame
, &cupsoptions
,
1068 lp_cups_options(talloc_tos(), lp_sub
, snum
), &size
)) {
1073 num_options
= cupsParseOptions(cupsoptions
, num_options
, &options
);
1076 cupsEncodeOptions(request
, num_options
, options
);
1079 * Do the request and get back a response...
1082 ustatus
= httpAssembleURIf(HTTP_URI_CODING_ALL
,
1086 NULL
, /* username */
1091 if (ustatus
!= HTTP_URI_STATUS_OK
) {
1095 if (!push_utf8_talloc(frame
, &filename
, pjob
->filename
, &size
)) {
1098 if ((response
= cupsDoFileRequest(http
, request
, uri
, pjob
->filename
)) != NULL
) {
1099 if (ippGetStatusCode(response
) >= IPP_OK_CONFLICT
) {
1100 DEBUG(0,("Unable to print file to %s - %s\n",
1101 lp_printername(talloc_tos(), lp_sub
, snum
),
1102 ippErrorString(cupsLastError())));
1105 attr_job_id
= ippFindAttribute(response
, "job-id", IPP_TAG_INTEGER
);
1107 pjob
->sysjob
= ippGetInteger(attr_job_id
, 0);
1108 DEBUG(5,("cups_job_submit: job-id %d\n", pjob
->sysjob
));
1110 DEBUG(0,("Missing job-id attribute in IPP response\n"));
1114 DEBUG(0,("Unable to print file to `%s' - %s\n",
1115 lp_printername(talloc_tos(), lp_sub
, snum
),
1116 ippErrorString(cupsLastError())));
1120 unlink(pjob
->filename
);
1121 /* else print_job_end will do it for us */
1125 ippDelete(response
);
1128 cupsLangFree(language
);
1139 * 'cups_queue_get()' - Get all the jobs in the print queue.
1142 static int cups_queue_get(const char *sharename
,
1143 enum printing_types printing_type
,
1145 print_queue_struct
**q
,
1146 print_status_struct
*status
)
1148 TALLOC_CTX
*frame
= talloc_stackframe();
1149 char *printername
= NULL
;
1150 http_t
*http
= NULL
; /* HTTP connection to server */
1151 ipp_t
*request
= NULL
, /* IPP Request */
1152 *response
= NULL
; /* IPP Response */
1153 ipp_attribute_t
*attr
= NULL
; /* Current attribute */
1154 cups_lang_t
*language
= NULL
; /* Default language */
1155 char uri
[HTTP_MAX_URI
] = {0}; /* printer-uri attribute */
1156 http_uri_status_t ustatus
;
1157 int qcount
= 0, /* Number of active queue entries */
1158 qalloc
= 0; /* Number of queue entries allocated */
1159 print_queue_struct
*queue
= NULL
, /* Queue entries */
1160 *temp
; /* Temporary pointer for queue */
1161 char *user_name
= NULL
, /* job-originating-user-name attribute */
1162 *job_name
= NULL
; /* job-name attribute */
1163 int job_id
; /* job-id attribute */
1164 int job_k_octets
; /* job-k-octets attribute */
1165 time_t job_time
; /* time-at-creation attribute */
1166 ipp_jstate_t job_status
; /* job-status attribute */
1167 int job_priority
; /* job-priority attribute */
1169 static const char *jattrs
[] = /* Requested job attributes */
1174 "job-originating-user-name",
1179 static const char *pattrs
[] = /* Requested printer attributes */
1182 "printer-state-message"
1187 /* HACK ALERT!!! The problem with support the 'printer name'
1188 option is that we key the tdb off the sharename. So we will
1189 overload the lpq_command string to pass in the printername
1190 (which is basically what we do for non-cups printers ... using
1191 the lpq_command to get the queue listing). */
1193 if (!push_utf8_talloc(frame
, &printername
, lpq_command
, &size
)) {
1196 DEBUG(5,("cups_queue_get(%s, %p, %p)\n", lpq_command
, q
, status
));
1199 * Make sure we don't ask for passwords...
1202 cupsSetPasswordCB(cups_passwd_cb
);
1205 * Try to connect to the server...
1208 if ((http
= cups_connect(frame
)) == NULL
) {
1213 * Generate the printer URI...
1216 ustatus
= httpAssembleURIf(HTTP_URI_CODING_ALL
,
1220 NULL
, /* username */
1225 if (ustatus
!= HTTP_URI_STATUS_OK
) {
1230 * Build an IPP_GET_JOBS request, which requires the following
1233 * attributes-charset
1234 * attributes-natural-language
1235 * requested-attributes
1241 ippSetOperation(request
, IPP_GET_JOBS
);
1242 ippSetRequestId(request
, 1);
1244 language
= cupsLangDefault();
1246 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1247 "attributes-charset", NULL
, "utf-8");
1249 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1250 "attributes-natural-language", NULL
, language
->language
);
1252 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1253 "requested-attributes",
1254 (sizeof(jattrs
) / sizeof(jattrs
[0])),
1257 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1258 "printer-uri", NULL
, uri
);
1261 * Do the request and get back a response...
1264 if ((response
= cupsDoRequest(http
, request
, "/")) == NULL
) {
1265 DEBUG(0,("Unable to get jobs for %s - %s\n", uri
,
1266 ippErrorString(cupsLastError())));
1270 if (ippGetStatusCode(response
) >= IPP_OK_CONFLICT
) {
1271 DEBUG(0,("Unable to get jobs for %s - %s\n", uri
,
1272 ippErrorString(ippGetStatusCode(response
))));
1277 * Process the jobs...
1284 for (attr
= ippFirstAttribute(response
); attr
!= NULL
; attr
= ippNextAttribute(response
)) {
1286 * Skip leading attributes until we hit a job...
1289 while (attr
!= NULL
&& ippGetGroupTag(attr
) != IPP_TAG_JOB
)
1290 attr
= ippNextAttribute(response
);
1296 * Allocate memory as needed...
1298 if (qcount
>= qalloc
) {
1301 queue
= SMB_REALLOC_ARRAY(queue
, print_queue_struct
, qalloc
);
1303 if (queue
== NULL
) {
1304 DEBUG(0,("cups_queue_get: Not enough memory!\n"));
1310 temp
= queue
+ qcount
;
1311 memset(temp
, 0, sizeof(print_queue_struct
));
1314 * Pull the needed attributes from this job...
1319 job_status
= IPP_JOB_PENDING
;
1325 while (attr
!= NULL
&& ippGetGroupTag(attr
) == IPP_TAG_JOB
) {
1326 if (ippGetName(attr
) == NULL
) {
1327 attr
= ippNextAttribute(response
);
1331 if (strcmp(ippGetName(attr
), "job-id") == 0 &&
1332 ippGetValueTag(attr
) == IPP_TAG_INTEGER
)
1333 job_id
= ippGetInteger(attr
, 0);
1335 if (strcmp(ippGetName(attr
), "job-k-octets") == 0 &&
1336 ippGetValueTag(attr
) == IPP_TAG_INTEGER
)
1337 job_k_octets
= ippGetInteger(attr
, 0);
1339 if (strcmp(ippGetName(attr
), "job-priority") == 0 &&
1340 ippGetValueTag(attr
) == IPP_TAG_INTEGER
)
1341 job_priority
= ippGetInteger(attr
, 0);
1343 if (strcmp(ippGetName(attr
), "job-state") == 0 &&
1344 ippGetValueTag(attr
) == IPP_TAG_ENUM
)
1345 job_status
= (ipp_jstate_t
)ippGetInteger(attr
, 0);
1347 if (strcmp(ippGetName(attr
), "time-at-creation") == 0 &&
1348 ippGetValueTag(attr
) == IPP_TAG_INTEGER
)
1349 job_time
= ippGetInteger(attr
, 0);
1351 if (strcmp(ippGetName(attr
), "job-name") == 0 &&
1352 ippGetValueTag(attr
) == IPP_TAG_NAME
) {
1353 if (!pull_utf8_talloc(frame
,
1355 ippGetString(attr
, 0, NULL
),
1361 if (strcmp(ippGetName(attr
), "job-originating-user-name") == 0 &&
1362 ippGetValueTag(attr
) == IPP_TAG_NAME
) {
1363 if (!pull_utf8_talloc(frame
,
1365 ippGetString(attr
, 0, NULL
),
1371 attr
= ippNextAttribute(response
);
1375 * See if we have everything needed...
1378 if (user_name
== NULL
|| job_name
== NULL
|| job_id
== 0) {
1385 temp
->sysjob
= job_id
;
1386 temp
->size
= job_k_octets
* 1024;
1387 temp
->status
= job_status
== IPP_JOB_PENDING
? LPQ_QUEUED
:
1388 job_status
== IPP_JOB_STOPPED
? LPQ_PAUSED
:
1389 job_status
== IPP_JOB_HELD
? LPQ_PAUSED
:
1391 temp
->priority
= job_priority
;
1392 temp
->time
= job_time
;
1393 strlcpy(temp
->fs_user
, user_name
, sizeof(temp
->fs_user
));
1394 strlcpy(temp
->fs_file
, job_name
, sizeof(temp
->fs_file
));
1402 ippDelete(response
);
1406 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the
1407 * following attributes:
1409 * attributes-charset
1410 * attributes-natural-language
1411 * requested-attributes
1417 ippSetOperation(request
, IPP_GET_PRINTER_ATTRIBUTES
);
1418 ippSetRequestId(request
, 1);
1420 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1421 "attributes-charset", NULL
, "utf-8");
1423 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1424 "attributes-natural-language", NULL
, language
->language
);
1426 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1427 "requested-attributes",
1428 (sizeof(pattrs
) / sizeof(pattrs
[0])),
1431 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1432 "printer-uri", NULL
, uri
);
1435 * Do the request and get back a response...
1438 if ((response
= cupsDoRequest(http
, request
, "/")) == NULL
) {
1439 DEBUG(0,("Unable to get printer status for %s - %s\n", printername
,
1440 ippErrorString(cupsLastError())));
1444 if (ippGetStatusCode(response
) >= IPP_OK_CONFLICT
) {
1445 DEBUG(0,("Unable to get printer status for %s - %s\n", printername
,
1446 ippErrorString(ippGetStatusCode(response
))));
1451 * Get the current printer status and convert it to the SAMBA values.
1454 if ((attr
= ippFindAttribute(response
, "printer-state", IPP_TAG_ENUM
)) != NULL
) {
1455 if (ippGetInteger(attr
, 0) == IPP_PRINTER_STOPPED
)
1456 status
->status
= LPSTAT_STOPPED
;
1458 status
->status
= LPSTAT_OK
;
1461 if ((attr
= ippFindAttribute(response
, "printer-state-message",
1462 IPP_TAG_TEXT
)) != NULL
) {
1464 if (!pull_utf8_talloc(frame
, &msg
,
1465 ippGetString(attr
, 0, NULL
),
1471 fstrcpy(status
->message
, msg
);
1477 * Return the job queue...
1483 ippDelete(response
);
1486 cupsLangFree(language
);
1497 * 'cups_queue_pause()' - Pause a print queue.
1500 static int cups_queue_pause(int snum
)
1502 TALLOC_CTX
*frame
= talloc_stackframe();
1503 const struct loadparm_substitution
*lp_sub
=
1504 loadparm_s3_global_substitution();
1505 int ret
= 1; /* Return value */
1506 http_t
*http
= NULL
; /* HTTP connection to server */
1507 ipp_t
*request
= NULL
, /* IPP Request */
1508 *response
= NULL
; /* IPP Response */
1509 cups_lang_t
*language
= NULL
; /* Default language */
1510 char *printername
= NULL
;
1511 char *username
= NULL
;
1512 char uri
[HTTP_MAX_URI
] = {0}; /* printer-uri attribute */
1513 http_uri_status_t ustatus
;
1516 DEBUG(5,("cups_queue_pause(%d)\n", snum
));
1519 * Make sure we don't ask for passwords...
1522 cupsSetPasswordCB(cups_passwd_cb
);
1525 * Try to connect to the server...
1528 if ((http
= cups_connect(frame
)) == NULL
) {
1533 * Build an IPP_PAUSE_PRINTER request, which requires the following
1536 * attributes-charset
1537 * attributes-natural-language
1539 * requesting-user-name
1544 ippSetOperation(request
, IPP_PAUSE_PRINTER
);
1545 ippSetRequestId(request
, 1);
1547 language
= cupsLangDefault();
1549 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1550 "attributes-charset", NULL
, "utf-8");
1552 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1553 "attributes-natural-language", NULL
, language
->language
);
1555 if (!push_utf8_talloc(frame
, &printername
,
1556 lp_printername(talloc_tos(), lp_sub
, snum
), &size
)) {
1559 ustatus
= httpAssembleURIf(HTTP_URI_CODING_ALL
,
1563 NULL
, /* username */
1568 if (ustatus
!= HTTP_URI_STATUS_OK
) {
1572 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
1574 if (!push_utf8_talloc(frame
, &username
, current_user_info
.unix_name
, &size
)) {
1577 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
1581 * Do the request and get back a response...
1584 if ((response
= cupsDoRequest(http
, request
, "/admin/")) != NULL
) {
1585 if (ippGetStatusCode(response
) >= IPP_OK_CONFLICT
) {
1586 DEBUG(0,("Unable to pause printer %s - %s\n",
1587 lp_printername(talloc_tos(), lp_sub
, snum
),
1588 ippErrorString(cupsLastError())));
1593 DEBUG(0,("Unable to pause printer %s - %s\n",
1594 lp_printername(talloc_tos(), lp_sub
, snum
),
1595 ippErrorString(cupsLastError())));
1600 ippDelete(response
);
1603 cupsLangFree(language
);
1614 * 'cups_queue_resume()' - Restart a print queue.
1617 static int cups_queue_resume(int snum
)
1619 TALLOC_CTX
*frame
= talloc_stackframe();
1620 const struct loadparm_substitution
*lp_sub
=
1621 loadparm_s3_global_substitution();
1622 int ret
= 1; /* Return value */
1623 http_t
*http
= NULL
; /* HTTP connection to server */
1624 ipp_t
*request
= NULL
, /* IPP Request */
1625 *response
= NULL
; /* IPP Response */
1626 cups_lang_t
*language
= NULL
; /* Default language */
1627 char *printername
= NULL
;
1628 char *username
= NULL
;
1629 char uri
[HTTP_MAX_URI
] = {0}; /* printer-uri attribute */
1630 http_uri_status_t ustatus
;
1633 DEBUG(5,("cups_queue_resume(%d)\n", snum
));
1636 * Make sure we don't ask for passwords...
1639 cupsSetPasswordCB(cups_passwd_cb
);
1642 * Try to connect to the server...
1645 if ((http
= cups_connect(frame
)) == NULL
) {
1650 * Build an IPP_RESUME_PRINTER request, which requires the following
1653 * attributes-charset
1654 * attributes-natural-language
1656 * requesting-user-name
1661 ippSetOperation(request
, IPP_RESUME_PRINTER
);
1662 ippSetRequestId(request
, 1);
1664 language
= cupsLangDefault();
1666 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1667 "attributes-charset", NULL
, "utf-8");
1669 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1670 "attributes-natural-language", NULL
, language
->language
);
1672 if (!push_utf8_talloc(frame
, &printername
, lp_printername(talloc_tos(), lp_sub
, snum
),
1676 ustatus
= httpAssembleURIf(HTTP_URI_CODING_ALL
,
1680 NULL
, /* username */
1685 if (ustatus
!= HTTP_URI_STATUS_OK
) {
1689 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
1691 if (!push_utf8_talloc(frame
, &username
, current_user_info
.unix_name
, &size
)) {
1694 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
1698 * Do the request and get back a response...
1701 if ((response
= cupsDoRequest(http
, request
, "/admin/")) != NULL
) {
1702 if (ippGetStatusCode(response
) >= IPP_OK_CONFLICT
) {
1703 DEBUG(0,("Unable to resume printer %s - %s\n",
1704 lp_printername(talloc_tos(), lp_sub
, snum
),
1705 ippErrorString(cupsLastError())));
1710 DEBUG(0,("Unable to resume printer %s - %s\n",
1711 lp_printername(talloc_tos(), lp_sub
, snum
),
1712 ippErrorString(cupsLastError())));
1717 ippDelete(response
);
1720 cupsLangFree(language
);
1729 /*******************************************************************
1730 * CUPS printing interface definitions...
1731 ******************************************************************/
1733 struct printif cups_printif
=
1746 /* this keeps fussy compilers happy */
1747 void print_cups_dummy(void);
1748 void print_cups_dummy(void) {}
1749 #endif /* HAVE_CUPS */