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"
30 #include <cups/cups.h>
31 #include <cups/language.h>
33 static SIG_ATOMIC_T gotalarm
;
35 /***************************************************************
36 Signal function to tell us we timed out.
37 ****************************************************************/
39 static void gotalarm_sig(int signum
)
44 extern userdom_struct current_user_info
;
47 * 'cups_passwd_cb()' - The CUPS password callback...
50 static const char * /* O - Password or NULL */
51 cups_passwd_cb(const char *prompt
) /* I - Prompt */
54 * Always return NULL to indicate that no password is available...
60 static http_t
*cups_connect(TALLOC_CTX
*frame
)
63 char *server
= NULL
, *p
= NULL
;
65 int timeout
= lp_cups_connection_timeout();
68 if (lp_cups_server() != NULL
&& strlen(lp_cups_server()) > 0) {
69 if (!push_utf8_talloc(frame
, &server
, lp_cups_server(), &size
)) {
73 server
= talloc_strdup(frame
,cupsServer());
79 p
= strchr(server
, ':');
87 DEBUG(10, ("connecting to cups server %s:%d\n",
93 CatchSignal(SIGALRM
, gotalarm_sig
);
97 #ifdef HAVE_HTTPCONNECTENCRYPT
98 http
= httpConnectEncrypt(server
, port
, lp_cups_encrypt());
100 http
= httpConnect(server
, port
);
104 CatchSignal(SIGALRM
, SIG_IGN
);
108 DEBUG(0,("Unable to connect to CUPS server %s:%d - %s\n",
109 server
, port
, strerror(errno
)));
115 static void send_pcap_info(const char *name
, const char *info
, void *pd
)
118 size_t namelen
= name
? strlen(name
)+1 : 0;
119 size_t infolen
= info
? strlen(info
)+1 : 0;
121 DEBUG(11,("send_pcap_info: writing namelen %u\n", (unsigned int)namelen
));
122 if (sys_write(fd
, &namelen
, sizeof(namelen
)) != sizeof(namelen
)) {
123 DEBUG(10,("send_pcap_info: namelen write failed %s\n",
127 DEBUG(11,("send_pcap_info: writing infolen %u\n", (unsigned int)infolen
));
128 if (sys_write(fd
, &infolen
, sizeof(infolen
)) != sizeof(infolen
)) {
129 DEBUG(10,("send_pcap_info: infolen write failed %s\n",
134 DEBUG(11,("send_pcap_info: writing name %s\n", name
));
135 if (sys_write(fd
, name
, namelen
) != namelen
) {
136 DEBUG(10,("send_pcap_info: name write failed %s\n",
142 DEBUG(11,("send_pcap_info: writing info %s\n", info
));
143 if (sys_write(fd
, info
, infolen
) != infolen
) {
144 DEBUG(10,("send_pcap_info: info write failed %s\n",
151 static bool cups_cache_reload_async(int fd
)
153 TALLOC_CTX
*frame
= talloc_stackframe();
154 struct pcap_cache
*tmp_pcap_cache
= NULL
;
155 http_t
*http
= NULL
; /* HTTP connection to server */
156 ipp_t
*request
= NULL
, /* IPP Request */
157 *response
= NULL
; /* IPP Response */
158 ipp_attribute_t
*attr
; /* Current attribute */
159 cups_lang_t
*language
= NULL
; /* Default language */
160 char *name
, /* printer-name attribute */
161 *info
; /* printer-info attribute */
162 static const char *requested
[] =/* Requested attributes */
170 DEBUG(5, ("reloading cups printcap cache\n"));
173 * Make sure we don't ask for passwords...
176 cupsSetPasswordCB(cups_passwd_cb
);
179 * Try to connect to the server...
182 if ((http
= cups_connect(frame
)) == NULL
) {
187 * Build a CUPS_GET_PRINTERS request, which requires the following
191 * attributes-natural-language
192 * requested-attributes
197 request
->request
.op
.operation_id
= CUPS_GET_PRINTERS
;
198 request
->request
.op
.request_id
= 1;
200 language
= cupsLangDefault();
202 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
203 "attributes-charset", NULL
, "utf-8");
205 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
206 "attributes-natural-language", NULL
, language
->language
);
208 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
209 "requested-attributes",
210 (sizeof(requested
) / sizeof(requested
[0])),
214 * Do the request and get back a response...
217 if ((response
= cupsDoRequest(http
, request
, "/")) == NULL
) {
218 DEBUG(0,("Unable to get printer list - %s\n",
219 ippErrorString(cupsLastError())));
223 for (attr
= response
->attrs
; attr
!= NULL
;) {
225 * Skip leading attributes until we hit a printer...
228 while (attr
!= NULL
&& attr
->group_tag
!= IPP_TAG_PRINTER
)
235 * Pull the needed attributes from this printer...
241 while (attr
!= NULL
&& attr
->group_tag
== IPP_TAG_PRINTER
) {
242 if (strcmp(attr
->name
, "printer-name") == 0 &&
243 attr
->value_tag
== IPP_TAG_NAME
) {
244 if (!pull_utf8_talloc(frame
,
246 attr
->values
[0].string
.text
,
252 if (strcmp(attr
->name
, "printer-info") == 0 &&
253 attr
->value_tag
== IPP_TAG_TEXT
) {
254 if (!pull_utf8_talloc(frame
,
256 attr
->values
[0].string
.text
,
266 * See if we have everything needed...
272 if (!pcap_cache_add_specific(&tmp_pcap_cache
, name
, info
)) {
281 * Build a CUPS_GET_CLASSES request, which requires the following
285 * attributes-natural-language
286 * requested-attributes
291 request
->request
.op
.operation_id
= CUPS_GET_CLASSES
;
292 request
->request
.op
.request_id
= 1;
294 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
295 "attributes-charset", NULL
, "utf-8");
297 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
298 "attributes-natural-language", NULL
, language
->language
);
300 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
301 "requested-attributes",
302 (sizeof(requested
) / sizeof(requested
[0])),
306 * Do the request and get back a response...
309 if ((response
= cupsDoRequest(http
, request
, "/")) == NULL
) {
310 DEBUG(0,("Unable to get printer list - %s\n",
311 ippErrorString(cupsLastError())));
315 for (attr
= response
->attrs
; attr
!= NULL
;) {
317 * Skip leading attributes until we hit a printer...
320 while (attr
!= NULL
&& attr
->group_tag
!= IPP_TAG_PRINTER
)
327 * Pull the needed attributes from this printer...
333 while (attr
!= NULL
&& attr
->group_tag
== IPP_TAG_PRINTER
) {
334 if (strcmp(attr
->name
, "printer-name") == 0 &&
335 attr
->value_tag
== IPP_TAG_NAME
) {
336 if (!pull_utf8_talloc(frame
,
338 attr
->values
[0].string
.text
,
344 if (strcmp(attr
->name
, "printer-info") == 0 &&
345 attr
->value_tag
== IPP_TAG_TEXT
) {
346 if (!pull_utf8_talloc(frame
,
348 attr
->values
[0].string
.text
,
358 * See if we have everything needed...
364 if (!pcap_cache_add_specific(&tmp_pcap_cache
, name
, info
)) {
376 cupsLangFree(language
);
381 /* Send all the entries up the pipe. */
382 if (tmp_pcap_cache
) {
383 pcap_printer_fn_specific(tmp_pcap_cache
,
387 pcap_cache_destroy_specific(&tmp_pcap_cache
);
393 static struct pcap_cache
*local_pcap_copy
;
394 struct fd_event
*cache_fd_event
;
396 static bool cups_pcap_load_async(struct tevent_context
*ev
,
397 struct messaging_context
*msg_ctx
,
406 if (cache_fd_event
) {
407 DEBUG(3,("cups_pcap_load_async: already waiting for "
408 "a refresh event\n" ));
412 DEBUG(5,("cups_pcap_load_async: asynchronously loading cups printers\n"));
414 if (pipe(fds
) == -1) {
419 if (pid
== (pid_t
)-1) {
420 DEBUG(10,("cups_pcap_load_async: fork failed %s\n",
428 DEBUG(10,("cups_pcap_load_async: child pid = %u\n",
429 (unsigned int)pid
));
438 close_all_print_db();
440 status
= reinit_after_fork(msg_ctx
, ev
, procid_self(), true);
441 if (!NT_STATUS_IS_OK(status
)) {
442 DEBUG(0,("cups_pcap_load_async: reinit_after_fork() failed\n"));
443 smb_panic("cups_pcap_load_async: reinit_after_fork() failed");
447 cups_cache_reload_async(fds
[1]);
452 struct cups_async_cb_args
{
454 struct event_context
*event_ctx
;
455 struct messaging_context
*msg_ctx
;
456 void (*post_cache_fill_fn
)(struct event_context
*,
457 struct messaging_context
*);
460 static void cups_async_callback(struct event_context
*event_ctx
,
461 struct fd_event
*event
,
465 TALLOC_CTX
*frame
= talloc_stackframe();
466 struct cups_async_cb_args
*cb_args
= (struct cups_async_cb_args
*)p
;
467 int fd
= cb_args
->pipe_fd
;
468 struct pcap_cache
*tmp_pcap_cache
= NULL
;
470 DEBUG(5,("cups_async_callback: callback received for printer data. "
474 char *name
= NULL
, *info
= NULL
;
475 size_t namelen
= 0, infolen
= 0;
478 ret
= sys_read(fd
, &namelen
, sizeof(namelen
));
483 if (ret
!= sizeof(namelen
)) {
484 DEBUG(10,("cups_async_callback: namelen read failed %d %s\n",
485 errno
, strerror(errno
)));
489 DEBUG(11,("cups_async_callback: read namelen %u\n",
490 (unsigned int)namelen
));
492 ret
= sys_read(fd
, &infolen
, sizeof(infolen
));
497 if (ret
!= sizeof(infolen
)) {
498 DEBUG(10,("cups_async_callback: infolen read failed %s\n",
503 DEBUG(11,("cups_async_callback: read infolen %u\n",
504 (unsigned int)infolen
));
507 name
= TALLOC_ARRAY(frame
, char, namelen
);
511 ret
= sys_read(fd
, name
, namelen
);
516 if (ret
!= namelen
) {
517 DEBUG(10,("cups_async_callback: name read failed %s\n",
521 DEBUG(11,("cups_async_callback: read name %s\n",
527 info
= TALLOC_ARRAY(frame
, char, infolen
);
531 ret
= sys_read(fd
, info
, infolen
);
536 if (ret
!= infolen
) {
537 DEBUG(10,("cups_async_callback: info read failed %s\n",
541 DEBUG(11,("cups_async_callback: read info %s\n",
547 /* Add to our local pcap cache. */
548 pcap_cache_add_specific(&tmp_pcap_cache
, name
, info
);
554 if (tmp_pcap_cache
) {
556 /* We got a namelist, replace our local cache. */
557 pcap_cache_destroy_specific(&local_pcap_copy
);
558 local_pcap_copy
= tmp_pcap_cache
;
560 /* And the systemwide pcap cache. */
561 ret
= pcap_cache_replace(local_pcap_copy
);
563 DEBUG(0, ("failed to replace pcap cache\n"));
565 /* Caller may have requested post cache fill callback */
566 if (ret
&& cb_args
->post_cache_fill_fn
!= NULL
) {
567 cb_args
->post_cache_fill_fn(cb_args
->event_ctx
,
571 DEBUG(2,("cups_async_callback: failed to read a new "
575 TALLOC_FREE(cb_args
);
576 TALLOC_FREE(cache_fd_event
);
579 bool cups_cache_reload(struct tevent_context
*ev
,
580 struct messaging_context
*msg_ctx
,
581 void (*post_cache_fill_fn
)(struct tevent_context
*,
582 struct messaging_context
*))
584 struct cups_async_cb_args
*cb_args
;
587 cb_args
= TALLOC_P(NULL
, struct cups_async_cb_args
);
588 if (cb_args
== NULL
) {
592 cb_args
->post_cache_fill_fn
= post_cache_fill_fn
;
593 cb_args
->event_ctx
= ev
;
594 cb_args
->msg_ctx
= msg_ctx
;
595 p_pipe_fd
= &cb_args
->pipe_fd
;
598 /* Set up an async refresh. */
599 if (!cups_pcap_load_async(ev
, msg_ctx
, p_pipe_fd
)) {
600 talloc_free(cb_args
);
603 if (!local_pcap_copy
) {
604 /* We have no local cache, wait directly for
605 * async refresh to complete.
607 DEBUG(10,("cups_cache_reload: sync read on fd %d\n",
610 cups_async_callback(ev
, NULL
,
613 if (!local_pcap_copy
) {
617 DEBUG(10,("cups_cache_reload: async read on fd %d\n",
620 /* Trigger an event when the pipe can be read. */
621 cache_fd_event
= event_add_fd(ev
,
626 if (!cache_fd_event
) {
628 TALLOC_FREE(cb_args
);
636 * 'cups_job_delete()' - Delete a job.
639 static int cups_job_delete(const char *sharename
, const char *lprm_command
, struct printjob
*pjob
)
641 TALLOC_CTX
*frame
= talloc_stackframe();
642 int ret
= 1; /* Return value */
643 http_t
*http
= NULL
; /* HTTP connection to server */
644 ipp_t
*request
= NULL
, /* IPP Request */
645 *response
= NULL
; /* IPP Response */
646 cups_lang_t
*language
= NULL
; /* Default language */
648 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
651 DEBUG(5,("cups_job_delete(%s, %p (%d))\n", sharename
, pjob
, pjob
->sysjob
));
654 * Make sure we don't ask for passwords...
657 cupsSetPasswordCB(cups_passwd_cb
);
660 * Try to connect to the server...
663 if ((http
= cups_connect(frame
)) == NULL
) {
668 * Build an IPP_CANCEL_JOB request, which requires the following
672 * attributes-natural-language
674 * requesting-user-name
679 request
->request
.op
.operation_id
= IPP_CANCEL_JOB
;
680 request
->request
.op
.request_id
= 1;
682 language
= cupsLangDefault();
684 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
685 "attributes-charset", NULL
, "utf-8");
687 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
688 "attributes-natural-language", NULL
, language
->language
);
690 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/jobs/%d", pjob
->sysjob
);
692 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
694 if (!push_utf8_talloc(frame
, &user
, pjob
->user
, &size
)) {
698 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
702 * Do the request and get back a response...
705 if ((response
= cupsDoRequest(http
, request
, "/jobs")) != NULL
) {
706 if (response
->request
.status
.status_code
>= IPP_OK_CONFLICT
) {
707 DEBUG(0,("Unable to cancel job %d - %s\n", pjob
->sysjob
,
708 ippErrorString(cupsLastError())));
713 DEBUG(0,("Unable to cancel job %d - %s\n", pjob
->sysjob
,
714 ippErrorString(cupsLastError())));
722 cupsLangFree(language
);
733 * 'cups_job_pause()' - Pause a job.
736 static int cups_job_pause(int snum
, struct printjob
*pjob
)
738 TALLOC_CTX
*frame
= talloc_stackframe();
739 int ret
= 1; /* Return value */
740 http_t
*http
= NULL
; /* HTTP connection to server */
741 ipp_t
*request
= NULL
, /* IPP Request */
742 *response
= NULL
; /* IPP Response */
743 cups_lang_t
*language
= NULL
; /* Default language */
745 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
748 DEBUG(5,("cups_job_pause(%d, %p (%d))\n", snum
, pjob
, pjob
->sysjob
));
751 * Make sure we don't ask for passwords...
754 cupsSetPasswordCB(cups_passwd_cb
);
757 * Try to connect to the server...
760 if ((http
= cups_connect(frame
)) == NULL
) {
765 * Build an IPP_HOLD_JOB request, which requires the following
769 * attributes-natural-language
771 * requesting-user-name
776 request
->request
.op
.operation_id
= IPP_HOLD_JOB
;
777 request
->request
.op
.request_id
= 1;
779 language
= cupsLangDefault();
781 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
782 "attributes-charset", NULL
, "utf-8");
784 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
785 "attributes-natural-language", NULL
, language
->language
);
787 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/jobs/%d", pjob
->sysjob
);
789 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
791 if (!push_utf8_talloc(frame
, &user
, pjob
->user
, &size
)) {
794 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
798 * Do the request and get back a response...
801 if ((response
= cupsDoRequest(http
, request
, "/jobs")) != NULL
) {
802 if (response
->request
.status
.status_code
>= IPP_OK_CONFLICT
) {
803 DEBUG(0,("Unable to hold job %d - %s\n", pjob
->sysjob
,
804 ippErrorString(cupsLastError())));
809 DEBUG(0,("Unable to hold job %d - %s\n", pjob
->sysjob
,
810 ippErrorString(cupsLastError())));
818 cupsLangFree(language
);
829 * 'cups_job_resume()' - Resume a paused job.
832 static int cups_job_resume(int snum
, struct printjob
*pjob
)
834 TALLOC_CTX
*frame
= talloc_stackframe();
835 int ret
= 1; /* Return value */
836 http_t
*http
= NULL
; /* HTTP connection to server */
837 ipp_t
*request
= NULL
, /* IPP Request */
838 *response
= NULL
; /* IPP Response */
839 cups_lang_t
*language
= NULL
; /* Default language */
841 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
844 DEBUG(5,("cups_job_resume(%d, %p (%d))\n", snum
, pjob
, pjob
->sysjob
));
847 * Make sure we don't ask for passwords...
850 cupsSetPasswordCB(cups_passwd_cb
);
853 * Try to connect to the server...
856 if ((http
= cups_connect(frame
)) == NULL
) {
861 * Build an IPP_RELEASE_JOB request, which requires the following
865 * attributes-natural-language
867 * requesting-user-name
872 request
->request
.op
.operation_id
= IPP_RELEASE_JOB
;
873 request
->request
.op
.request_id
= 1;
875 language
= cupsLangDefault();
877 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
878 "attributes-charset", NULL
, "utf-8");
880 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
881 "attributes-natural-language", NULL
, language
->language
);
883 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/jobs/%d", pjob
->sysjob
);
885 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
887 if (!push_utf8_talloc(frame
, &user
, pjob
->user
, &size
)) {
890 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
894 * Do the request and get back a response...
897 if ((response
= cupsDoRequest(http
, request
, "/jobs")) != NULL
) {
898 if (response
->request
.status
.status_code
>= IPP_OK_CONFLICT
) {
899 DEBUG(0,("Unable to release job %d - %s\n", pjob
->sysjob
,
900 ippErrorString(cupsLastError())));
905 DEBUG(0,("Unable to release job %d - %s\n", pjob
->sysjob
,
906 ippErrorString(cupsLastError())));
914 cupsLangFree(language
);
925 * 'cups_job_submit()' - Submit a job for printing.
928 static int cups_job_submit(int snum
, struct printjob
*pjob
)
930 TALLOC_CTX
*frame
= talloc_stackframe();
931 int ret
= 1; /* Return value */
932 http_t
*http
= NULL
; /* HTTP connection to server */
933 ipp_t
*request
= NULL
, /* IPP Request */
934 *response
= NULL
; /* IPP Response */
935 ipp_attribute_t
*attr_job_id
= NULL
; /* IPP Attribute "job-id" */
936 cups_lang_t
*language
= NULL
; /* Default language */
937 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
938 char *new_jobname
= NULL
;
940 cups_option_t
*options
= NULL
;
941 char *printername
= NULL
;
943 char *jobname
= NULL
;
944 char *cupsoptions
= NULL
;
945 char *filename
= NULL
;
947 uint32_t jobid
= (uint32_t)-1;
949 DEBUG(5,("cups_job_submit(%d, %p)\n", snum
, pjob
));
952 * Make sure we don't ask for passwords...
955 cupsSetPasswordCB(cups_passwd_cb
);
958 * Try to connect to the server...
961 if ((http
= cups_connect(frame
)) == NULL
) {
966 * Build an IPP_PRINT_JOB request, which requires the following
970 * attributes-natural-language
972 * requesting-user-name
978 request
->request
.op
.operation_id
= IPP_PRINT_JOB
;
979 request
->request
.op
.request_id
= 1;
981 language
= cupsLangDefault();
983 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
984 "attributes-charset", NULL
, "utf-8");
986 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
987 "attributes-natural-language", NULL
, language
->language
);
989 if (!push_utf8_talloc(frame
, &printername
, lp_printername(snum
),
993 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/printers/%s",
996 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
997 "printer-uri", NULL
, uri
);
999 if (!push_utf8_talloc(frame
, &user
, pjob
->user
, &size
)) {
1002 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
1005 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1006 "job-originating-host-name", NULL
,
1007 pjob
->clientmachine
);
1009 /* Get the jobid from the filename. */
1010 jobid
= print_parse_jobid(pjob
->filename
);
1011 if (jobid
== (uint32_t)-1) {
1012 DEBUG(0,("cups_job_submit: failed to parse jobid from name %s\n",
1017 if (!push_utf8_talloc(frame
, &jobname
, pjob
->jobname
, &size
)) {
1020 new_jobname
= talloc_asprintf(frame
,
1021 "%s%.8u %s", PRINT_SPOOL_PREFIX
,
1022 (unsigned int)jobid
,
1024 if (new_jobname
== NULL
) {
1028 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name", NULL
,
1032 * add any options defined in smb.conf
1035 if (!push_utf8_talloc(frame
, &cupsoptions
, lp_cups_options(snum
), &size
)) {
1040 num_options
= cupsParseOptions(cupsoptions
, num_options
, &options
);
1043 cupsEncodeOptions(request
, num_options
, options
);
1046 * Do the request and get back a response...
1049 slprintf(uri
, sizeof(uri
) - 1, "/printers/%s", printername
);
1051 if (!push_utf8_talloc(frame
, &filename
, pjob
->filename
, &size
)) {
1054 if ((response
= cupsDoFileRequest(http
, request
, uri
, pjob
->filename
)) != NULL
) {
1055 if (response
->request
.status
.status_code
>= IPP_OK_CONFLICT
) {
1056 DEBUG(0,("Unable to print file to %s - %s\n",
1057 lp_printername(snum
),
1058 ippErrorString(cupsLastError())));
1061 attr_job_id
= ippFindAttribute(response
, "job-id", IPP_TAG_INTEGER
);
1063 pjob
->sysjob
= attr_job_id
->values
[0].integer
;
1064 DEBUG(5,("cups_job_submit: job-id %d\n", pjob
->sysjob
));
1066 DEBUG(0,("Missing job-id attribute in IPP response"));
1070 DEBUG(0,("Unable to print file to `%s' - %s\n",
1071 lp_printername(snum
),
1072 ippErrorString(cupsLastError())));
1076 unlink(pjob
->filename
);
1077 /* else print_job_end will do it for us */
1081 ippDelete(response
);
1084 cupsLangFree(language
);
1095 * 'cups_queue_get()' - Get all the jobs in the print queue.
1098 static int cups_queue_get(const char *sharename
,
1099 enum printing_types printing_type
,
1101 print_queue_struct
**q
,
1102 print_status_struct
*status
)
1104 TALLOC_CTX
*frame
= talloc_stackframe();
1105 char *printername
= NULL
;
1106 http_t
*http
= NULL
; /* HTTP connection to server */
1107 ipp_t
*request
= NULL
, /* IPP Request */
1108 *response
= NULL
; /* IPP Response */
1109 ipp_attribute_t
*attr
= NULL
; /* Current attribute */
1110 cups_lang_t
*language
= NULL
; /* Default language */
1111 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
1112 int qcount
= 0, /* Number of active queue entries */
1113 qalloc
= 0; /* Number of queue entries allocated */
1114 print_queue_struct
*queue
= NULL
, /* Queue entries */
1115 *temp
; /* Temporary pointer for queue */
1116 char *user_name
= NULL
, /* job-originating-user-name attribute */
1117 *job_name
= NULL
; /* job-name attribute */
1118 int job_id
; /* job-id attribute */
1119 int job_k_octets
; /* job-k-octets attribute */
1120 time_t job_time
; /* time-at-creation attribute */
1121 ipp_jstate_t job_status
; /* job-status attribute */
1122 int job_priority
; /* job-priority attribute */
1124 static const char *jattrs
[] = /* Requested job attributes */
1129 "job-originating-user-name",
1134 static const char *pattrs
[] = /* Requested printer attributes */
1137 "printer-state-message"
1142 /* HACK ALERT!!! The problem with support the 'printer name'
1143 option is that we key the tdb off the sharename. So we will
1144 overload the lpq_command string to pass in the printername
1145 (which is basically what we do for non-cups printers ... using
1146 the lpq_command to get the queue listing). */
1148 if (!push_utf8_talloc(frame
, &printername
, lpq_command
, &size
)) {
1151 DEBUG(5,("cups_queue_get(%s, %p, %p)\n", lpq_command
, q
, status
));
1154 * Make sure we don't ask for passwords...
1157 cupsSetPasswordCB(cups_passwd_cb
);
1160 * Try to connect to the server...
1163 if ((http
= cups_connect(frame
)) == NULL
) {
1168 * Generate the printer URI...
1171 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/printers/%s", printername
);
1174 * Build an IPP_GET_JOBS request, which requires the following
1177 * attributes-charset
1178 * attributes-natural-language
1179 * requested-attributes
1185 request
->request
.op
.operation_id
= IPP_GET_JOBS
;
1186 request
->request
.op
.request_id
= 1;
1188 language
= cupsLangDefault();
1190 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1191 "attributes-charset", NULL
, "utf-8");
1193 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1194 "attributes-natural-language", NULL
, language
->language
);
1196 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1197 "requested-attributes",
1198 (sizeof(jattrs
) / sizeof(jattrs
[0])),
1201 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1202 "printer-uri", NULL
, uri
);
1205 * Do the request and get back a response...
1208 if ((response
= cupsDoRequest(http
, request
, "/")) == NULL
) {
1209 DEBUG(0,("Unable to get jobs for %s - %s\n", uri
,
1210 ippErrorString(cupsLastError())));
1214 if (response
->request
.status
.status_code
>= IPP_OK_CONFLICT
) {
1215 DEBUG(0,("Unable to get jobs for %s - %s\n", uri
,
1216 ippErrorString(response
->request
.status
.status_code
)));
1221 * Process the jobs...
1228 for (attr
= response
->attrs
; attr
!= NULL
; attr
= attr
->next
) {
1230 * Skip leading attributes until we hit a job...
1233 while (attr
!= NULL
&& attr
->group_tag
!= IPP_TAG_JOB
)
1240 * Allocate memory as needed...
1242 if (qcount
>= qalloc
) {
1245 queue
= SMB_REALLOC_ARRAY(queue
, print_queue_struct
, qalloc
);
1247 if (queue
== NULL
) {
1248 DEBUG(0,("cups_queue_get: Not enough memory!"));
1254 temp
= queue
+ qcount
;
1255 memset(temp
, 0, sizeof(print_queue_struct
));
1258 * Pull the needed attributes from this job...
1263 job_status
= IPP_JOB_PENDING
;
1269 while (attr
!= NULL
&& attr
->group_tag
== IPP_TAG_JOB
) {
1270 if (attr
->name
== NULL
) {
1275 if (strcmp(attr
->name
, "job-id") == 0 &&
1276 attr
->value_tag
== IPP_TAG_INTEGER
)
1277 job_id
= attr
->values
[0].integer
;
1279 if (strcmp(attr
->name
, "job-k-octets") == 0 &&
1280 attr
->value_tag
== IPP_TAG_INTEGER
)
1281 job_k_octets
= attr
->values
[0].integer
;
1283 if (strcmp(attr
->name
, "job-priority") == 0 &&
1284 attr
->value_tag
== IPP_TAG_INTEGER
)
1285 job_priority
= attr
->values
[0].integer
;
1287 if (strcmp(attr
->name
, "job-state") == 0 &&
1288 attr
->value_tag
== IPP_TAG_ENUM
)
1289 job_status
= (ipp_jstate_t
)(attr
->values
[0].integer
);
1291 if (strcmp(attr
->name
, "time-at-creation") == 0 &&
1292 attr
->value_tag
== IPP_TAG_INTEGER
)
1293 job_time
= attr
->values
[0].integer
;
1295 if (strcmp(attr
->name
, "job-name") == 0 &&
1296 attr
->value_tag
== IPP_TAG_NAME
) {
1297 if (!pull_utf8_talloc(frame
,
1299 attr
->values
[0].string
.text
,
1305 if (strcmp(attr
->name
, "job-originating-user-name") == 0 &&
1306 attr
->value_tag
== IPP_TAG_NAME
) {
1307 if (!pull_utf8_talloc(frame
,
1309 attr
->values
[0].string
.text
,
1319 * See if we have everything needed...
1322 if (user_name
== NULL
|| job_name
== NULL
|| job_id
== 0) {
1330 temp
->size
= job_k_octets
* 1024;
1331 temp
->status
= job_status
== IPP_JOB_PENDING
? LPQ_QUEUED
:
1332 job_status
== IPP_JOB_STOPPED
? LPQ_PAUSED
:
1333 job_status
== IPP_JOB_HELD
? LPQ_PAUSED
:
1335 temp
->priority
= job_priority
;
1336 temp
->time
= job_time
;
1337 strlcpy(temp
->fs_user
, user_name
, sizeof(temp
->fs_user
));
1338 strlcpy(temp
->fs_file
, job_name
, sizeof(temp
->fs_file
));
1346 ippDelete(response
);
1350 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the
1351 * following attributes:
1353 * attributes-charset
1354 * attributes-natural-language
1355 * requested-attributes
1361 request
->request
.op
.operation_id
= IPP_GET_PRINTER_ATTRIBUTES
;
1362 request
->request
.op
.request_id
= 1;
1364 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1365 "attributes-charset", NULL
, "utf-8");
1367 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1368 "attributes-natural-language", NULL
, language
->language
);
1370 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1371 "requested-attributes",
1372 (sizeof(pattrs
) / sizeof(pattrs
[0])),
1375 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1376 "printer-uri", NULL
, uri
);
1379 * Do the request and get back a response...
1382 if ((response
= cupsDoRequest(http
, request
, "/")) == NULL
) {
1383 DEBUG(0,("Unable to get printer status for %s - %s\n", printername
,
1384 ippErrorString(cupsLastError())));
1388 if (response
->request
.status
.status_code
>= IPP_OK_CONFLICT
) {
1389 DEBUG(0,("Unable to get printer status for %s - %s\n", printername
,
1390 ippErrorString(response
->request
.status
.status_code
)));
1395 * Get the current printer status and convert it to the SAMBA values.
1398 if ((attr
= ippFindAttribute(response
, "printer-state", IPP_TAG_ENUM
)) != NULL
) {
1399 if (attr
->values
[0].integer
== IPP_PRINTER_STOPPED
)
1400 status
->status
= LPSTAT_STOPPED
;
1402 status
->status
= LPSTAT_OK
;
1405 if ((attr
= ippFindAttribute(response
, "printer-state-message",
1406 IPP_TAG_TEXT
)) != NULL
) {
1408 if (!pull_utf8_talloc(frame
, &msg
,
1409 attr
->values
[0].string
.text
,
1415 fstrcpy(status
->message
, msg
);
1421 * Return the job queue...
1427 ippDelete(response
);
1430 cupsLangFree(language
);
1441 * 'cups_queue_pause()' - Pause a print queue.
1444 static int cups_queue_pause(int snum
)
1446 TALLOC_CTX
*frame
= talloc_stackframe();
1447 int ret
= 1; /* Return value */
1448 http_t
*http
= NULL
; /* HTTP connection to server */
1449 ipp_t
*request
= NULL
, /* IPP Request */
1450 *response
= NULL
; /* IPP Response */
1451 cups_lang_t
*language
= NULL
; /* Default language */
1452 char *printername
= NULL
;
1453 char *username
= NULL
;
1454 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
1457 DEBUG(5,("cups_queue_pause(%d)\n", snum
));
1460 * Make sure we don't ask for passwords...
1463 cupsSetPasswordCB(cups_passwd_cb
);
1466 * Try to connect to the server...
1469 if ((http
= cups_connect(frame
)) == NULL
) {
1474 * Build an IPP_PAUSE_PRINTER request, which requires the following
1477 * attributes-charset
1478 * attributes-natural-language
1480 * requesting-user-name
1485 request
->request
.op
.operation_id
= IPP_PAUSE_PRINTER
;
1486 request
->request
.op
.request_id
= 1;
1488 language
= cupsLangDefault();
1490 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1491 "attributes-charset", NULL
, "utf-8");
1493 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1494 "attributes-natural-language", NULL
, language
->language
);
1496 if (!push_utf8_talloc(frame
, &printername
, lp_printername(snum
),
1500 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/printers/%s",
1503 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
1505 if (!push_utf8_talloc(frame
, &username
, current_user_info
.unix_name
, &size
)) {
1508 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
1512 * Do the request and get back a response...
1515 if ((response
= cupsDoRequest(http
, request
, "/admin/")) != NULL
) {
1516 if (response
->request
.status
.status_code
>= IPP_OK_CONFLICT
) {
1517 DEBUG(0,("Unable to pause printer %s - %s\n",
1518 lp_printername(snum
),
1519 ippErrorString(cupsLastError())));
1524 DEBUG(0,("Unable to pause printer %s - %s\n",
1525 lp_printername(snum
),
1526 ippErrorString(cupsLastError())));
1531 ippDelete(response
);
1534 cupsLangFree(language
);
1545 * 'cups_queue_resume()' - Restart a print queue.
1548 static int cups_queue_resume(int snum
)
1550 TALLOC_CTX
*frame
= talloc_stackframe();
1551 int ret
= 1; /* Return value */
1552 http_t
*http
= NULL
; /* HTTP connection to server */
1553 ipp_t
*request
= NULL
, /* IPP Request */
1554 *response
= NULL
; /* IPP Response */
1555 cups_lang_t
*language
= NULL
; /* Default language */
1556 char *printername
= NULL
;
1557 char *username
= NULL
;
1558 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
1561 DEBUG(5,("cups_queue_resume(%d)\n", snum
));
1564 * Make sure we don't ask for passwords...
1567 cupsSetPasswordCB(cups_passwd_cb
);
1570 * Try to connect to the server...
1573 if ((http
= cups_connect(frame
)) == NULL
) {
1578 * Build an IPP_RESUME_PRINTER request, which requires the following
1581 * attributes-charset
1582 * attributes-natural-language
1584 * requesting-user-name
1589 request
->request
.op
.operation_id
= IPP_RESUME_PRINTER
;
1590 request
->request
.op
.request_id
= 1;
1592 language
= cupsLangDefault();
1594 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1595 "attributes-charset", NULL
, "utf-8");
1597 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1598 "attributes-natural-language", NULL
, language
->language
);
1600 if (!push_utf8_talloc(frame
, &printername
, lp_printername(snum
),
1604 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/printers/%s",
1607 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
1609 if (!push_utf8_talloc(frame
, &username
, current_user_info
.unix_name
, &size
)) {
1612 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
1616 * Do the request and get back a response...
1619 if ((response
= cupsDoRequest(http
, request
, "/admin/")) != NULL
) {
1620 if (response
->request
.status
.status_code
>= IPP_OK_CONFLICT
) {
1621 DEBUG(0,("Unable to resume printer %s - %s\n",
1622 lp_printername(snum
),
1623 ippErrorString(cupsLastError())));
1628 DEBUG(0,("Unable to resume printer %s - %s\n",
1629 lp_printername(snum
),
1630 ippErrorString(cupsLastError())));
1635 ippDelete(response
);
1638 cupsLangFree(language
);
1647 /*******************************************************************
1648 * CUPS printing interface definitions...
1649 ******************************************************************/
1651 struct printif cups_printif
=
1663 bool cups_pull_comment_location(TALLOC_CTX
*mem_ctx
,
1664 const char *printername
,
1668 TALLOC_CTX
*frame
= talloc_stackframe();
1669 http_t
*http
= NULL
; /* HTTP connection to server */
1670 ipp_t
*request
= NULL
, /* IPP Request */
1671 *response
= NULL
; /* IPP Response */
1672 ipp_attribute_t
*attr
; /* Current attribute */
1673 cups_lang_t
*language
= NULL
; /* Default language */
1674 char uri
[HTTP_MAX_URI
];
1675 char *server
= NULL
;
1676 char *sharename
= NULL
;
1678 static const char *requested
[] =/* Requested attributes */
1687 DEBUG(5, ("pulling %s location\n", printername
));
1690 * Make sure we don't ask for passwords...
1693 cupsSetPasswordCB(cups_passwd_cb
);
1696 * Try to connect to the server...
1699 if ((http
= cups_connect(frame
)) == NULL
) {
1705 request
->request
.op
.operation_id
= IPP_GET_PRINTER_ATTRIBUTES
;
1706 request
->request
.op
.request_id
= 1;
1708 language
= cupsLangDefault();
1710 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1711 "attributes-charset", NULL
, "utf-8");
1713 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1714 "attributes-natural-language", NULL
, language
->language
);
1716 if (lp_cups_server() != NULL
&& strlen(lp_cups_server()) > 0) {
1717 if (!push_utf8_talloc(frame
, &server
, lp_cups_server(), &size
)) {
1721 server
= talloc_strdup(frame
,cupsServer());
1726 if (!push_utf8_talloc(frame
, &sharename
, printername
, &size
)) {
1729 slprintf(uri
, sizeof(uri
) - 1, "ipp://%s/printers/%s",
1732 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1733 "printer-uri", NULL
, uri
);
1735 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1736 "requested-attributes",
1737 (sizeof(requested
) / sizeof(requested
[0])),
1741 * Do the request and get back a response...
1744 if ((response
= cupsDoRequest(http
, request
, "/")) == NULL
) {
1745 DEBUG(0,("Unable to get printer attributes - %s\n",
1746 ippErrorString(cupsLastError())));
1750 for (attr
= response
->attrs
; attr
!= NULL
;) {
1752 * Skip leading attributes until we hit a printer...
1755 while (attr
!= NULL
&& attr
->group_tag
!= IPP_TAG_PRINTER
)
1762 * Pull the needed attributes from this printer...
1765 while ( attr
&& (attr
->group_tag
== IPP_TAG_PRINTER
) ) {
1766 if (strcmp(attr
->name
, "printer-name") == 0 &&
1767 attr
->value_tag
== IPP_TAG_NAME
) {
1768 if (!pull_utf8_talloc(frame
,
1770 attr
->values
[0].string
.text
,
1776 /* Grab the comment if we don't have one */
1777 if ( (strcmp(attr
->name
, "printer-info") == 0)
1778 && (attr
->value_tag
== IPP_TAG_TEXT
))
1780 if (!pull_utf8_talloc(mem_ctx
,
1782 attr
->values
[0].string
.text
,
1786 DEBUG(5,("cups_pull_comment_location: Using cups comment: %s\n",
1790 /* Grab the location if we don't have one */
1791 if ( (strcmp(attr
->name
, "printer-location") == 0)
1792 && (attr
->value_tag
== IPP_TAG_TEXT
))
1794 if (!pull_utf8_talloc(mem_ctx
,
1796 attr
->values
[0].string
.text
,
1800 DEBUG(5,("cups_pull_comment_location: Using cups location: %s\n",
1808 * We have everything needed...
1819 ippDelete(response
);
1826 cupsLangFree(language
);
1836 /* this keeps fussy compilers happy */
1837 void print_cups_dummy(void);
1838 void print_cups_dummy(void) {}
1839 #endif /* HAVE_CUPS */