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 fd_event
*cache_fd_event
;
395 static bool cups_pcap_load_async(struct tevent_context
*ev
,
396 struct messaging_context
*msg_ctx
,
405 if (cache_fd_event
) {
406 DEBUG(3,("cups_pcap_load_async: already waiting for "
407 "a refresh event\n" ));
411 DEBUG(5,("cups_pcap_load_async: asynchronously loading cups printers\n"));
413 if (pipe(fds
) == -1) {
418 if (pid
== (pid_t
)-1) {
419 DEBUG(10,("cups_pcap_load_async: fork failed %s\n",
427 DEBUG(10,("cups_pcap_load_async: child pid = %u\n",
428 (unsigned int)pid
));
437 close_all_print_db();
439 status
= reinit_after_fork(msg_ctx
, ev
, procid_self(), true);
440 if (!NT_STATUS_IS_OK(status
)) {
441 DEBUG(0,("cups_pcap_load_async: reinit_after_fork() failed\n"));
442 smb_panic("cups_pcap_load_async: reinit_after_fork() failed");
446 cups_cache_reload_async(fds
[1]);
451 struct cups_async_cb_args
{
453 struct event_context
*event_ctx
;
454 struct messaging_context
*msg_ctx
;
455 void (*post_cache_fill_fn
)(struct event_context
*,
456 struct messaging_context
*);
459 static void cups_async_callback(struct event_context
*event_ctx
,
460 struct fd_event
*event
,
464 TALLOC_CTX
*frame
= talloc_stackframe();
465 struct cups_async_cb_args
*cb_args
= (struct cups_async_cb_args
*)p
;
466 int fd
= cb_args
->pipe_fd
;
467 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
)));
490 DEBUG(11,("cups_async_callback: read namelen %u\n",
491 (unsigned int)namelen
));
493 ret
= sys_read(fd
, &infolen
, sizeof(infolen
));
498 if (ret
!= sizeof(infolen
)) {
499 DEBUG(10,("cups_async_callback: infolen read failed %s\n",
505 DEBUG(11,("cups_async_callback: read infolen %u\n",
506 (unsigned int)infolen
));
509 name
= TALLOC_ARRAY(frame
, char, namelen
);
514 ret
= sys_read(fd
, name
, namelen
);
519 if (ret
!= namelen
) {
520 DEBUG(10,("cups_async_callback: name read failed %s\n",
525 DEBUG(11,("cups_async_callback: read name %s\n",
531 info
= TALLOC_ARRAY(frame
, char, infolen
);
536 ret
= sys_read(fd
, info
, infolen
);
541 if (ret
!= infolen
) {
542 DEBUG(10,("cups_async_callback: info read failed %s\n",
547 DEBUG(11,("cups_async_callback: read info %s\n",
553 /* Add to our local pcap cache. */
554 ret_ok
= pcap_cache_add_specific(&tmp_pcap_cache
, name
, info
);
558 DEBUG(0, ("failed to add to tmp pcap cache\n"));
565 DEBUG(0, ("failed to read a new printer list\n"));
566 pcap_cache_destroy_specific(&tmp_pcap_cache
);
569 * replace the system-wide pcap cache with a (possibly empty)
571 * FIXME The child process does not currently propagate cups
572 * errors back up to the parent, therefore we cannot
573 * differentiate between an empty printer list and a failure.
575 ret_ok
= pcap_cache_replace(tmp_pcap_cache
);
577 DEBUG(0, ("failed to replace pcap cache\n"));
578 } else if (cb_args
->post_cache_fill_fn
!= NULL
) {
579 /* Caller requested post cache fill callback */
580 cb_args
->post_cache_fill_fn(cb_args
->event_ctx
,
585 TALLOC_FREE(cb_args
);
586 TALLOC_FREE(cache_fd_event
);
589 bool cups_cache_reload(struct tevent_context
*ev
,
590 struct messaging_context
*msg_ctx
,
591 void (*post_cache_fill_fn
)(struct tevent_context
*,
592 struct messaging_context
*))
594 struct cups_async_cb_args
*cb_args
;
597 cb_args
= TALLOC_P(NULL
, struct cups_async_cb_args
);
598 if (cb_args
== NULL
) {
602 cb_args
->post_cache_fill_fn
= post_cache_fill_fn
;
603 cb_args
->event_ctx
= ev
;
604 cb_args
->msg_ctx
= msg_ctx
;
605 p_pipe_fd
= &cb_args
->pipe_fd
;
608 /* Set up an async refresh. */
609 if (!cups_pcap_load_async(ev
, msg_ctx
, p_pipe_fd
)) {
610 talloc_free(cb_args
);
614 DEBUG(10,("cups_cache_reload: async read on fd %d\n",
617 /* Trigger an event when the pipe can be read. */
618 cache_fd_event
= event_add_fd(ev
,
623 if (!cache_fd_event
) {
625 TALLOC_FREE(cb_args
);
633 * 'cups_job_delete()' - Delete a job.
636 static int cups_job_delete(const char *sharename
, const char *lprm_command
, struct printjob
*pjob
)
638 TALLOC_CTX
*frame
= talloc_stackframe();
639 int ret
= 1; /* Return value */
640 http_t
*http
= NULL
; /* HTTP connection to server */
641 ipp_t
*request
= NULL
, /* IPP Request */
642 *response
= NULL
; /* IPP Response */
643 cups_lang_t
*language
= NULL
; /* Default language */
645 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
648 DEBUG(5,("cups_job_delete(%s, %p (%d))\n", sharename
, pjob
, pjob
->sysjob
));
651 * Make sure we don't ask for passwords...
654 cupsSetPasswordCB(cups_passwd_cb
);
657 * Try to connect to the server...
660 if ((http
= cups_connect(frame
)) == NULL
) {
665 * Build an IPP_CANCEL_JOB request, which requires the following
669 * attributes-natural-language
671 * requesting-user-name
676 request
->request
.op
.operation_id
= IPP_CANCEL_JOB
;
677 request
->request
.op
.request_id
= 1;
679 language
= cupsLangDefault();
681 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
682 "attributes-charset", NULL
, "utf-8");
684 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
685 "attributes-natural-language", NULL
, language
->language
);
687 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/jobs/%d", pjob
->sysjob
);
689 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
691 if (!push_utf8_talloc(frame
, &user
, pjob
->user
, &size
)) {
695 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
699 * Do the request and get back a response...
702 if ((response
= cupsDoRequest(http
, request
, "/jobs")) != NULL
) {
703 if (response
->request
.status
.status_code
>= IPP_OK_CONFLICT
) {
704 DEBUG(0,("Unable to cancel job %d - %s\n", pjob
->sysjob
,
705 ippErrorString(cupsLastError())));
710 DEBUG(0,("Unable to cancel job %d - %s\n", pjob
->sysjob
,
711 ippErrorString(cupsLastError())));
719 cupsLangFree(language
);
730 * 'cups_job_pause()' - Pause a job.
733 static int cups_job_pause(int snum
, struct printjob
*pjob
)
735 TALLOC_CTX
*frame
= talloc_stackframe();
736 int ret
= 1; /* Return value */
737 http_t
*http
= NULL
; /* HTTP connection to server */
738 ipp_t
*request
= NULL
, /* IPP Request */
739 *response
= NULL
; /* IPP Response */
740 cups_lang_t
*language
= NULL
; /* Default language */
742 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
745 DEBUG(5,("cups_job_pause(%d, %p (%d))\n", snum
, pjob
, pjob
->sysjob
));
748 * Make sure we don't ask for passwords...
751 cupsSetPasswordCB(cups_passwd_cb
);
754 * Try to connect to the server...
757 if ((http
= cups_connect(frame
)) == NULL
) {
762 * Build an IPP_HOLD_JOB request, which requires the following
766 * attributes-natural-language
768 * requesting-user-name
773 request
->request
.op
.operation_id
= IPP_HOLD_JOB
;
774 request
->request
.op
.request_id
= 1;
776 language
= cupsLangDefault();
778 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
779 "attributes-charset", NULL
, "utf-8");
781 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
782 "attributes-natural-language", NULL
, language
->language
);
784 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/jobs/%d", pjob
->sysjob
);
786 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
788 if (!push_utf8_talloc(frame
, &user
, pjob
->user
, &size
)) {
791 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
795 * Do the request and get back a response...
798 if ((response
= cupsDoRequest(http
, request
, "/jobs")) != NULL
) {
799 if (response
->request
.status
.status_code
>= IPP_OK_CONFLICT
) {
800 DEBUG(0,("Unable to hold job %d - %s\n", pjob
->sysjob
,
801 ippErrorString(cupsLastError())));
806 DEBUG(0,("Unable to hold job %d - %s\n", pjob
->sysjob
,
807 ippErrorString(cupsLastError())));
815 cupsLangFree(language
);
826 * 'cups_job_resume()' - Resume a paused job.
829 static int cups_job_resume(int snum
, struct printjob
*pjob
)
831 TALLOC_CTX
*frame
= talloc_stackframe();
832 int ret
= 1; /* Return value */
833 http_t
*http
= NULL
; /* HTTP connection to server */
834 ipp_t
*request
= NULL
, /* IPP Request */
835 *response
= NULL
; /* IPP Response */
836 cups_lang_t
*language
= NULL
; /* Default language */
838 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
841 DEBUG(5,("cups_job_resume(%d, %p (%d))\n", snum
, pjob
, pjob
->sysjob
));
844 * Make sure we don't ask for passwords...
847 cupsSetPasswordCB(cups_passwd_cb
);
850 * Try to connect to the server...
853 if ((http
= cups_connect(frame
)) == NULL
) {
858 * Build an IPP_RELEASE_JOB request, which requires the following
862 * attributes-natural-language
864 * requesting-user-name
869 request
->request
.op
.operation_id
= IPP_RELEASE_JOB
;
870 request
->request
.op
.request_id
= 1;
872 language
= cupsLangDefault();
874 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
875 "attributes-charset", NULL
, "utf-8");
877 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
878 "attributes-natural-language", NULL
, language
->language
);
880 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/jobs/%d", pjob
->sysjob
);
882 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
884 if (!push_utf8_talloc(frame
, &user
, pjob
->user
, &size
)) {
887 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
891 * Do the request and get back a response...
894 if ((response
= cupsDoRequest(http
, request
, "/jobs")) != NULL
) {
895 if (response
->request
.status
.status_code
>= IPP_OK_CONFLICT
) {
896 DEBUG(0,("Unable to release job %d - %s\n", pjob
->sysjob
,
897 ippErrorString(cupsLastError())));
902 DEBUG(0,("Unable to release job %d - %s\n", pjob
->sysjob
,
903 ippErrorString(cupsLastError())));
911 cupsLangFree(language
);
922 * 'cups_job_submit()' - Submit a job for printing.
925 static int cups_job_submit(int snum
, struct printjob
*pjob
)
927 TALLOC_CTX
*frame
= talloc_stackframe();
928 int ret
= 1; /* Return value */
929 http_t
*http
= NULL
; /* HTTP connection to server */
930 ipp_t
*request
= NULL
, /* IPP Request */
931 *response
= NULL
; /* IPP Response */
932 ipp_attribute_t
*attr_job_id
= NULL
; /* IPP Attribute "job-id" */
933 cups_lang_t
*language
= NULL
; /* Default language */
934 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
935 char *new_jobname
= NULL
;
937 cups_option_t
*options
= NULL
;
938 char *printername
= NULL
;
940 char *jobname
= NULL
;
941 char *cupsoptions
= NULL
;
942 char *filename
= NULL
;
944 uint32_t jobid
= (uint32_t)-1;
946 DEBUG(5,("cups_job_submit(%d, %p)\n", snum
, pjob
));
949 * Make sure we don't ask for passwords...
952 cupsSetPasswordCB(cups_passwd_cb
);
955 * Try to connect to the server...
958 if ((http
= cups_connect(frame
)) == NULL
) {
963 * Build an IPP_PRINT_JOB request, which requires the following
967 * attributes-natural-language
969 * requesting-user-name
975 request
->request
.op
.operation_id
= IPP_PRINT_JOB
;
976 request
->request
.op
.request_id
= 1;
978 language
= cupsLangDefault();
980 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
981 "attributes-charset", NULL
, "utf-8");
983 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
984 "attributes-natural-language", NULL
, language
->language
);
986 if (!push_utf8_talloc(frame
, &printername
, lp_printername(snum
),
990 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/printers/%s",
993 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
994 "printer-uri", NULL
, uri
);
996 if (!push_utf8_talloc(frame
, &user
, pjob
->user
, &size
)) {
999 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
1002 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1003 "job-originating-host-name", NULL
,
1004 pjob
->clientmachine
);
1006 /* Get the jobid from the filename. */
1007 jobid
= print_parse_jobid(pjob
->filename
);
1008 if (jobid
== (uint32_t)-1) {
1009 DEBUG(0,("cups_job_submit: failed to parse jobid from name %s\n",
1014 if (!push_utf8_talloc(frame
, &jobname
, pjob
->jobname
, &size
)) {
1017 new_jobname
= talloc_asprintf(frame
,
1018 "%s%.8u %s", PRINT_SPOOL_PREFIX
,
1019 (unsigned int)jobid
,
1021 if (new_jobname
== NULL
) {
1025 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name", NULL
,
1029 * add any options defined in smb.conf
1032 if (!push_utf8_talloc(frame
, &cupsoptions
, lp_cups_options(snum
), &size
)) {
1037 num_options
= cupsParseOptions(cupsoptions
, num_options
, &options
);
1040 cupsEncodeOptions(request
, num_options
, options
);
1043 * Do the request and get back a response...
1046 slprintf(uri
, sizeof(uri
) - 1, "/printers/%s", printername
);
1048 if (!push_utf8_talloc(frame
, &filename
, pjob
->filename
, &size
)) {
1051 if ((response
= cupsDoFileRequest(http
, request
, uri
, pjob
->filename
)) != NULL
) {
1052 if (response
->request
.status
.status_code
>= IPP_OK_CONFLICT
) {
1053 DEBUG(0,("Unable to print file to %s - %s\n",
1054 lp_printername(snum
),
1055 ippErrorString(cupsLastError())));
1058 attr_job_id
= ippFindAttribute(response
, "job-id", IPP_TAG_INTEGER
);
1060 pjob
->sysjob
= attr_job_id
->values
[0].integer
;
1061 DEBUG(5,("cups_job_submit: job-id %d\n", pjob
->sysjob
));
1063 DEBUG(0,("Missing job-id attribute in IPP response"));
1067 DEBUG(0,("Unable to print file to `%s' - %s\n",
1068 lp_printername(snum
),
1069 ippErrorString(cupsLastError())));
1073 unlink(pjob
->filename
);
1074 /* else print_job_end will do it for us */
1078 ippDelete(response
);
1081 cupsLangFree(language
);
1092 * 'cups_queue_get()' - Get all the jobs in the print queue.
1095 static int cups_queue_get(const char *sharename
,
1096 enum printing_types printing_type
,
1098 print_queue_struct
**q
,
1099 print_status_struct
*status
)
1101 TALLOC_CTX
*frame
= talloc_stackframe();
1102 char *printername
= NULL
;
1103 http_t
*http
= NULL
; /* HTTP connection to server */
1104 ipp_t
*request
= NULL
, /* IPP Request */
1105 *response
= NULL
; /* IPP Response */
1106 ipp_attribute_t
*attr
= NULL
; /* Current attribute */
1107 cups_lang_t
*language
= NULL
; /* Default language */
1108 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
1109 int qcount
= 0, /* Number of active queue entries */
1110 qalloc
= 0; /* Number of queue entries allocated */
1111 print_queue_struct
*queue
= NULL
, /* Queue entries */
1112 *temp
; /* Temporary pointer for queue */
1113 char *user_name
= NULL
, /* job-originating-user-name attribute */
1114 *job_name
= NULL
; /* job-name attribute */
1115 int job_id
; /* job-id attribute */
1116 int job_k_octets
; /* job-k-octets attribute */
1117 time_t job_time
; /* time-at-creation attribute */
1118 ipp_jstate_t job_status
; /* job-status attribute */
1119 int job_priority
; /* job-priority attribute */
1121 static const char *jattrs
[] = /* Requested job attributes */
1126 "job-originating-user-name",
1131 static const char *pattrs
[] = /* Requested printer attributes */
1134 "printer-state-message"
1139 /* HACK ALERT!!! The problem with support the 'printer name'
1140 option is that we key the tdb off the sharename. So we will
1141 overload the lpq_command string to pass in the printername
1142 (which is basically what we do for non-cups printers ... using
1143 the lpq_command to get the queue listing). */
1145 if (!push_utf8_talloc(frame
, &printername
, lpq_command
, &size
)) {
1148 DEBUG(5,("cups_queue_get(%s, %p, %p)\n", lpq_command
, q
, status
));
1151 * Make sure we don't ask for passwords...
1154 cupsSetPasswordCB(cups_passwd_cb
);
1157 * Try to connect to the server...
1160 if ((http
= cups_connect(frame
)) == NULL
) {
1165 * Generate the printer URI...
1168 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/printers/%s", printername
);
1171 * Build an IPP_GET_JOBS request, which requires the following
1174 * attributes-charset
1175 * attributes-natural-language
1176 * requested-attributes
1182 request
->request
.op
.operation_id
= IPP_GET_JOBS
;
1183 request
->request
.op
.request_id
= 1;
1185 language
= cupsLangDefault();
1187 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1188 "attributes-charset", NULL
, "utf-8");
1190 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1191 "attributes-natural-language", NULL
, language
->language
);
1193 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1194 "requested-attributes",
1195 (sizeof(jattrs
) / sizeof(jattrs
[0])),
1198 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1199 "printer-uri", NULL
, uri
);
1202 * Do the request and get back a response...
1205 if ((response
= cupsDoRequest(http
, request
, "/")) == NULL
) {
1206 DEBUG(0,("Unable to get jobs for %s - %s\n", uri
,
1207 ippErrorString(cupsLastError())));
1211 if (response
->request
.status
.status_code
>= IPP_OK_CONFLICT
) {
1212 DEBUG(0,("Unable to get jobs for %s - %s\n", uri
,
1213 ippErrorString(response
->request
.status
.status_code
)));
1218 * Process the jobs...
1225 for (attr
= response
->attrs
; attr
!= NULL
; attr
= attr
->next
) {
1227 * Skip leading attributes until we hit a job...
1230 while (attr
!= NULL
&& attr
->group_tag
!= IPP_TAG_JOB
)
1237 * Allocate memory as needed...
1239 if (qcount
>= qalloc
) {
1242 queue
= SMB_REALLOC_ARRAY(queue
, print_queue_struct
, qalloc
);
1244 if (queue
== NULL
) {
1245 DEBUG(0,("cups_queue_get: Not enough memory!"));
1251 temp
= queue
+ qcount
;
1252 memset(temp
, 0, sizeof(print_queue_struct
));
1255 * Pull the needed attributes from this job...
1260 job_status
= IPP_JOB_PENDING
;
1266 while (attr
!= NULL
&& attr
->group_tag
== IPP_TAG_JOB
) {
1267 if (attr
->name
== NULL
) {
1272 if (strcmp(attr
->name
, "job-id") == 0 &&
1273 attr
->value_tag
== IPP_TAG_INTEGER
)
1274 job_id
= attr
->values
[0].integer
;
1276 if (strcmp(attr
->name
, "job-k-octets") == 0 &&
1277 attr
->value_tag
== IPP_TAG_INTEGER
)
1278 job_k_octets
= attr
->values
[0].integer
;
1280 if (strcmp(attr
->name
, "job-priority") == 0 &&
1281 attr
->value_tag
== IPP_TAG_INTEGER
)
1282 job_priority
= attr
->values
[0].integer
;
1284 if (strcmp(attr
->name
, "job-state") == 0 &&
1285 attr
->value_tag
== IPP_TAG_ENUM
)
1286 job_status
= (ipp_jstate_t
)(attr
->values
[0].integer
);
1288 if (strcmp(attr
->name
, "time-at-creation") == 0 &&
1289 attr
->value_tag
== IPP_TAG_INTEGER
)
1290 job_time
= attr
->values
[0].integer
;
1292 if (strcmp(attr
->name
, "job-name") == 0 &&
1293 attr
->value_tag
== IPP_TAG_NAME
) {
1294 if (!pull_utf8_talloc(frame
,
1296 attr
->values
[0].string
.text
,
1302 if (strcmp(attr
->name
, "job-originating-user-name") == 0 &&
1303 attr
->value_tag
== IPP_TAG_NAME
) {
1304 if (!pull_utf8_talloc(frame
,
1306 attr
->values
[0].string
.text
,
1316 * See if we have everything needed...
1319 if (user_name
== NULL
|| job_name
== NULL
|| job_id
== 0) {
1327 temp
->size
= job_k_octets
* 1024;
1328 temp
->status
= job_status
== IPP_JOB_PENDING
? LPQ_QUEUED
:
1329 job_status
== IPP_JOB_STOPPED
? LPQ_PAUSED
:
1330 job_status
== IPP_JOB_HELD
? LPQ_PAUSED
:
1332 temp
->priority
= job_priority
;
1333 temp
->time
= job_time
;
1334 strlcpy(temp
->fs_user
, user_name
, sizeof(temp
->fs_user
));
1335 strlcpy(temp
->fs_file
, job_name
, sizeof(temp
->fs_file
));
1343 ippDelete(response
);
1347 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the
1348 * following attributes:
1350 * attributes-charset
1351 * attributes-natural-language
1352 * requested-attributes
1358 request
->request
.op
.operation_id
= IPP_GET_PRINTER_ATTRIBUTES
;
1359 request
->request
.op
.request_id
= 1;
1361 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1362 "attributes-charset", NULL
, "utf-8");
1364 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1365 "attributes-natural-language", NULL
, language
->language
);
1367 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1368 "requested-attributes",
1369 (sizeof(pattrs
) / sizeof(pattrs
[0])),
1372 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1373 "printer-uri", NULL
, uri
);
1376 * Do the request and get back a response...
1379 if ((response
= cupsDoRequest(http
, request
, "/")) == NULL
) {
1380 DEBUG(0,("Unable to get printer status for %s - %s\n", printername
,
1381 ippErrorString(cupsLastError())));
1385 if (response
->request
.status
.status_code
>= IPP_OK_CONFLICT
) {
1386 DEBUG(0,("Unable to get printer status for %s - %s\n", printername
,
1387 ippErrorString(response
->request
.status
.status_code
)));
1392 * Get the current printer status and convert it to the SAMBA values.
1395 if ((attr
= ippFindAttribute(response
, "printer-state", IPP_TAG_ENUM
)) != NULL
) {
1396 if (attr
->values
[0].integer
== IPP_PRINTER_STOPPED
)
1397 status
->status
= LPSTAT_STOPPED
;
1399 status
->status
= LPSTAT_OK
;
1402 if ((attr
= ippFindAttribute(response
, "printer-state-message",
1403 IPP_TAG_TEXT
)) != NULL
) {
1405 if (!pull_utf8_talloc(frame
, &msg
,
1406 attr
->values
[0].string
.text
,
1412 fstrcpy(status
->message
, msg
);
1418 * Return the job queue...
1424 ippDelete(response
);
1427 cupsLangFree(language
);
1438 * 'cups_queue_pause()' - Pause a print queue.
1441 static int cups_queue_pause(int snum
)
1443 TALLOC_CTX
*frame
= talloc_stackframe();
1444 int ret
= 1; /* Return value */
1445 http_t
*http
= NULL
; /* HTTP connection to server */
1446 ipp_t
*request
= NULL
, /* IPP Request */
1447 *response
= NULL
; /* IPP Response */
1448 cups_lang_t
*language
= NULL
; /* Default language */
1449 char *printername
= NULL
;
1450 char *username
= NULL
;
1451 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
1454 DEBUG(5,("cups_queue_pause(%d)\n", snum
));
1457 * Make sure we don't ask for passwords...
1460 cupsSetPasswordCB(cups_passwd_cb
);
1463 * Try to connect to the server...
1466 if ((http
= cups_connect(frame
)) == NULL
) {
1471 * Build an IPP_PAUSE_PRINTER request, which requires the following
1474 * attributes-charset
1475 * attributes-natural-language
1477 * requesting-user-name
1482 request
->request
.op
.operation_id
= IPP_PAUSE_PRINTER
;
1483 request
->request
.op
.request_id
= 1;
1485 language
= cupsLangDefault();
1487 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1488 "attributes-charset", NULL
, "utf-8");
1490 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1491 "attributes-natural-language", NULL
, language
->language
);
1493 if (!push_utf8_talloc(frame
, &printername
, lp_printername(snum
),
1497 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/printers/%s",
1500 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
1502 if (!push_utf8_talloc(frame
, &username
, current_user_info
.unix_name
, &size
)) {
1505 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
1509 * Do the request and get back a response...
1512 if ((response
= cupsDoRequest(http
, request
, "/admin/")) != NULL
) {
1513 if (response
->request
.status
.status_code
>= IPP_OK_CONFLICT
) {
1514 DEBUG(0,("Unable to pause printer %s - %s\n",
1515 lp_printername(snum
),
1516 ippErrorString(cupsLastError())));
1521 DEBUG(0,("Unable to pause printer %s - %s\n",
1522 lp_printername(snum
),
1523 ippErrorString(cupsLastError())));
1528 ippDelete(response
);
1531 cupsLangFree(language
);
1542 * 'cups_queue_resume()' - Restart a print queue.
1545 static int cups_queue_resume(int snum
)
1547 TALLOC_CTX
*frame
= talloc_stackframe();
1548 int ret
= 1; /* Return value */
1549 http_t
*http
= NULL
; /* HTTP connection to server */
1550 ipp_t
*request
= NULL
, /* IPP Request */
1551 *response
= NULL
; /* IPP Response */
1552 cups_lang_t
*language
= NULL
; /* Default language */
1553 char *printername
= NULL
;
1554 char *username
= NULL
;
1555 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
1558 DEBUG(5,("cups_queue_resume(%d)\n", snum
));
1561 * Make sure we don't ask for passwords...
1564 cupsSetPasswordCB(cups_passwd_cb
);
1567 * Try to connect to the server...
1570 if ((http
= cups_connect(frame
)) == NULL
) {
1575 * Build an IPP_RESUME_PRINTER request, which requires the following
1578 * attributes-charset
1579 * attributes-natural-language
1581 * requesting-user-name
1586 request
->request
.op
.operation_id
= IPP_RESUME_PRINTER
;
1587 request
->request
.op
.request_id
= 1;
1589 language
= cupsLangDefault();
1591 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1592 "attributes-charset", NULL
, "utf-8");
1594 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1595 "attributes-natural-language", NULL
, language
->language
);
1597 if (!push_utf8_talloc(frame
, &printername
, lp_printername(snum
),
1601 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/printers/%s",
1604 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
1606 if (!push_utf8_talloc(frame
, &username
, current_user_info
.unix_name
, &size
)) {
1609 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
1613 * Do the request and get back a response...
1616 if ((response
= cupsDoRequest(http
, request
, "/admin/")) != NULL
) {
1617 if (response
->request
.status
.status_code
>= IPP_OK_CONFLICT
) {
1618 DEBUG(0,("Unable to resume printer %s - %s\n",
1619 lp_printername(snum
),
1620 ippErrorString(cupsLastError())));
1625 DEBUG(0,("Unable to resume printer %s - %s\n",
1626 lp_printername(snum
),
1627 ippErrorString(cupsLastError())));
1632 ippDelete(response
);
1635 cupsLangFree(language
);
1644 /*******************************************************************
1645 * CUPS printing interface definitions...
1646 ******************************************************************/
1648 struct printif cups_printif
=
1660 bool cups_pull_comment_location(TALLOC_CTX
*mem_ctx
,
1661 const char *printername
,
1665 TALLOC_CTX
*frame
= talloc_stackframe();
1666 http_t
*http
= NULL
; /* HTTP connection to server */
1667 ipp_t
*request
= NULL
, /* IPP Request */
1668 *response
= NULL
; /* IPP Response */
1669 ipp_attribute_t
*attr
; /* Current attribute */
1670 cups_lang_t
*language
= NULL
; /* Default language */
1671 char uri
[HTTP_MAX_URI
];
1672 char *server
= NULL
;
1673 char *sharename
= NULL
;
1675 static const char *requested
[] =/* Requested attributes */
1684 DEBUG(5, ("pulling %s location\n", printername
));
1687 * Make sure we don't ask for passwords...
1690 cupsSetPasswordCB(cups_passwd_cb
);
1693 * Try to connect to the server...
1696 if ((http
= cups_connect(frame
)) == NULL
) {
1702 request
->request
.op
.operation_id
= IPP_GET_PRINTER_ATTRIBUTES
;
1703 request
->request
.op
.request_id
= 1;
1705 language
= cupsLangDefault();
1707 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1708 "attributes-charset", NULL
, "utf-8");
1710 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1711 "attributes-natural-language", NULL
, language
->language
);
1713 if (lp_cups_server() != NULL
&& strlen(lp_cups_server()) > 0) {
1714 if (!push_utf8_talloc(frame
, &server
, lp_cups_server(), &size
)) {
1718 server
= talloc_strdup(frame
,cupsServer());
1723 if (!push_utf8_talloc(frame
, &sharename
, printername
, &size
)) {
1726 slprintf(uri
, sizeof(uri
) - 1, "ipp://%s/printers/%s",
1729 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1730 "printer-uri", NULL
, uri
);
1732 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1733 "requested-attributes",
1734 (sizeof(requested
) / sizeof(requested
[0])),
1738 * Do the request and get back a response...
1741 if ((response
= cupsDoRequest(http
, request
, "/")) == NULL
) {
1742 DEBUG(0,("Unable to get printer attributes - %s\n",
1743 ippErrorString(cupsLastError())));
1747 for (attr
= response
->attrs
; attr
!= NULL
;) {
1749 * Skip leading attributes until we hit a printer...
1752 while (attr
!= NULL
&& attr
->group_tag
!= IPP_TAG_PRINTER
)
1759 * Pull the needed attributes from this printer...
1762 while ( attr
&& (attr
->group_tag
== IPP_TAG_PRINTER
) ) {
1763 if (strcmp(attr
->name
, "printer-name") == 0 &&
1764 attr
->value_tag
== IPP_TAG_NAME
) {
1765 if (!pull_utf8_talloc(frame
,
1767 attr
->values
[0].string
.text
,
1773 /* Grab the comment if we don't have one */
1774 if ( (strcmp(attr
->name
, "printer-info") == 0)
1775 && (attr
->value_tag
== IPP_TAG_TEXT
))
1777 if (!pull_utf8_talloc(mem_ctx
,
1779 attr
->values
[0].string
.text
,
1783 DEBUG(5,("cups_pull_comment_location: Using cups comment: %s\n",
1787 /* Grab the location if we don't have one */
1788 if ( (strcmp(attr
->name
, "printer-location") == 0)
1789 && (attr
->value_tag
== IPP_TAG_TEXT
))
1791 if (!pull_utf8_talloc(mem_ctx
,
1793 attr
->values
[0].string
.text
,
1797 DEBUG(5,("cups_pull_comment_location: Using cups location: %s\n",
1805 * We have everything needed...
1816 ippDelete(response
);
1823 cupsLangFree(language
);
1833 /* this keeps fussy compilers happy */
1834 void print_cups_dummy(void);
1835 void print_cups_dummy(void) {}
1836 #endif /* HAVE_CUPS */