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.
29 #include <cups/cups.h>
30 #include <cups/language.h>
32 static SIG_ATOMIC_T gotalarm
;
34 /***************************************************************
35 Signal function to tell us we timed out.
36 ****************************************************************/
38 static void gotalarm_sig(void)
43 extern userdom_struct current_user_info
;
46 * 'cups_passwd_cb()' - The CUPS password callback...
49 static const char * /* O - Password or NULL */
50 cups_passwd_cb(const char *prompt
) /* I - Prompt */
53 * Always return NULL to indicate that no password is available...
59 static http_t
*cups_connect(TALLOC_CTX
*frame
)
62 char *server
= NULL
, *p
= NULL
;
64 int timeout
= lp_cups_connection_timeout();
67 if (lp_cups_server() != NULL
&& strlen(lp_cups_server()) > 0) {
68 if (!push_utf8_talloc(frame
, &server
, lp_cups_server(), &size
)) {
72 server
= talloc_strdup(frame
,cupsServer());
78 p
= strchr(server
, ':');
86 DEBUG(10, ("connecting to cups server %s:%d\n",
92 CatchSignal(SIGALRM
, SIGNAL_CAST gotalarm_sig
);
96 #ifdef HAVE_HTTPCONNECTENCRYPT
97 http
= httpConnectEncrypt(server
, port
, lp_cups_encrypt());
99 http
= httpConnect(server
, port
);
103 CatchSignal(SIGALRM
, SIGNAL_CAST SIG_IGN
);
107 DEBUG(0,("Unable to connect to CUPS server %s:%d - %s\n",
108 server
, port
, strerror(errno
)));
114 static void send_pcap_info(const char *name
, const char *info
, void *pd
)
117 size_t namelen
= name
? strlen(name
)+1 : 0;
118 size_t infolen
= info
? strlen(info
)+1 : 0;
120 DEBUG(11,("send_pcap_info: writing namelen %u\n", (unsigned int)namelen
));
121 if (sys_write(fd
, &namelen
, sizeof(namelen
)) != sizeof(namelen
)) {
122 DEBUG(10,("send_pcap_info: namelen write failed %s\n",
126 DEBUG(11,("send_pcap_info: writing infolen %u\n", (unsigned int)infolen
));
127 if (sys_write(fd
, &infolen
, sizeof(infolen
)) != sizeof(infolen
)) {
128 DEBUG(10,("send_pcap_info: infolen write failed %s\n",
133 DEBUG(11,("send_pcap_info: writing name %s\n", name
));
134 if (sys_write(fd
, name
, namelen
) != namelen
) {
135 DEBUG(10,("send_pcap_info: name write failed %s\n",
141 DEBUG(11,("send_pcap_info: writing info %s\n", info
));
142 if (sys_write(fd
, info
, infolen
) != infolen
) {
143 DEBUG(10,("send_pcap_info: info write failed %s\n",
150 static bool cups_cache_reload_async(int fd
)
152 TALLOC_CTX
*frame
= talloc_stackframe();
153 struct pcap_cache
*tmp_pcap_cache
= NULL
;
154 http_t
*http
= NULL
; /* HTTP connection to server */
155 ipp_t
*request
= NULL
, /* IPP Request */
156 *response
= NULL
; /* IPP Response */
157 ipp_attribute_t
*attr
; /* Current attribute */
158 cups_lang_t
*language
= NULL
; /* Default language */
159 char *name
, /* printer-name attribute */
160 *info
; /* printer-info attribute */
161 static const char *requested
[] =/* Requested attributes */
169 DEBUG(5, ("reloading cups printcap cache\n"));
172 * Make sure we don't ask for passwords...
175 cupsSetPasswordCB(cups_passwd_cb
);
178 * Try to connect to the server...
181 if ((http
= cups_connect(frame
)) == NULL
) {
186 * Build a CUPS_GET_PRINTERS request, which requires the following
190 * attributes-natural-language
191 * requested-attributes
196 request
->request
.op
.operation_id
= CUPS_GET_PRINTERS
;
197 request
->request
.op
.request_id
= 1;
199 language
= cupsLangDefault();
201 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
202 "attributes-charset", NULL
, "utf-8");
204 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
205 "attributes-natural-language", NULL
, language
->language
);
207 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
208 "requested-attributes",
209 (sizeof(requested
) / sizeof(requested
[0])),
213 * Do the request and get back a response...
216 if ((response
= cupsDoRequest(http
, request
, "/")) == NULL
) {
217 DEBUG(0,("Unable to get printer list - %s\n",
218 ippErrorString(cupsLastError())));
222 for (attr
= response
->attrs
; attr
!= NULL
;) {
224 * Skip leading attributes until we hit a printer...
227 while (attr
!= NULL
&& attr
->group_tag
!= IPP_TAG_PRINTER
)
234 * Pull the needed attributes from this printer...
240 while (attr
!= NULL
&& attr
->group_tag
== IPP_TAG_PRINTER
) {
241 if (strcmp(attr
->name
, "printer-name") == 0 &&
242 attr
->value_tag
== IPP_TAG_NAME
) {
243 if (!pull_utf8_talloc(frame
,
245 attr
->values
[0].string
.text
,
251 if (strcmp(attr
->name
, "printer-info") == 0 &&
252 attr
->value_tag
== IPP_TAG_TEXT
) {
253 if (!pull_utf8_talloc(frame
,
255 attr
->values
[0].string
.text
,
265 * See if we have everything needed...
271 if (!pcap_cache_add_specific(&tmp_pcap_cache
, name
, info
)) {
280 * Build a CUPS_GET_CLASSES request, which requires the following
284 * attributes-natural-language
285 * requested-attributes
290 request
->request
.op
.operation_id
= CUPS_GET_CLASSES
;
291 request
->request
.op
.request_id
= 1;
293 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
294 "attributes-charset", NULL
, "utf-8");
296 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
297 "attributes-natural-language", NULL
, language
->language
);
299 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
300 "requested-attributes",
301 (sizeof(requested
) / sizeof(requested
[0])),
305 * Do the request and get back a response...
308 if ((response
= cupsDoRequest(http
, request
, "/")) == NULL
) {
309 DEBUG(0,("Unable to get printer list - %s\n",
310 ippErrorString(cupsLastError())));
314 for (attr
= response
->attrs
; attr
!= NULL
;) {
316 * Skip leading attributes until we hit a printer...
319 while (attr
!= NULL
&& attr
->group_tag
!= IPP_TAG_PRINTER
)
326 * Pull the needed attributes from this printer...
332 while (attr
!= NULL
&& attr
->group_tag
== IPP_TAG_PRINTER
) {
333 if (strcmp(attr
->name
, "printer-name") == 0 &&
334 attr
->value_tag
== IPP_TAG_NAME
) {
335 if (!pull_utf8_talloc(frame
,
337 attr
->values
[0].string
.text
,
343 if (strcmp(attr
->name
, "printer-info") == 0 &&
344 attr
->value_tag
== IPP_TAG_TEXT
) {
345 if (!pull_utf8_talloc(frame
,
347 attr
->values
[0].string
.text
,
357 * See if we have everything needed...
363 if (!pcap_cache_add_specific(&tmp_pcap_cache
, name
, info
)) {
375 cupsLangFree(language
);
380 /* Send all the entries up the pipe. */
381 if (tmp_pcap_cache
) {
382 pcap_printer_fn_specific(tmp_pcap_cache
,
386 pcap_cache_destroy_specific(&tmp_pcap_cache
);
392 static struct pcap_cache
*local_pcap_copy
;
393 struct fd_event
*cache_fd_event
;
395 static bool cups_pcap_load_async(int *pfd
)
402 if (cache_fd_event
) {
403 DEBUG(3,("cups_pcap_load_async: already waiting for "
404 "a refresh event\n" ));
408 DEBUG(5,("cups_pcap_load_async: asynchronously loading cups printers\n"));
410 if (pipe(fds
) == -1) {
415 if (pid
== (pid_t
)-1) {
416 DEBUG(10,("cups_pcap_load_async: fork failed %s\n",
424 DEBUG(10,("cups_pcap_load_async: child pid = %u\n",
425 (unsigned int)pid
));
434 close_all_print_db();
436 if (!NT_STATUS_IS_OK(reinit_after_fork(smbd_messaging_context(),
437 smbd_event_context(), true))) {
438 DEBUG(0,("cups_pcap_load_async: reinit_after_fork() failed\n"));
439 smb_panic("cups_pcap_load_async: reinit_after_fork() failed");
443 cups_cache_reload_async(fds
[1]);
448 struct cups_async_cb_args
{
450 void (*post_cache_fill_fn
)(void);
453 static void cups_async_callback(struct event_context
*event_ctx
,
454 struct fd_event
*event
,
458 TALLOC_CTX
*frame
= talloc_stackframe();
459 struct cups_async_cb_args
*cb_args
= (struct cups_async_cb_args
*)p
;
460 int fd
= cb_args
->pipe_fd
;
461 struct pcap_cache
*tmp_pcap_cache
= NULL
;
463 DEBUG(5,("cups_async_callback: callback received for printer data. "
467 char *name
= NULL
, *info
= NULL
;
468 size_t namelen
= 0, infolen
= 0;
471 ret
= sys_read(fd
, &namelen
, sizeof(namelen
));
476 if (ret
!= sizeof(namelen
)) {
477 DEBUG(10,("cups_async_callback: namelen read failed %d %s\n",
478 errno
, strerror(errno
)));
482 DEBUG(11,("cups_async_callback: read namelen %u\n",
483 (unsigned int)namelen
));
485 ret
= sys_read(fd
, &infolen
, sizeof(infolen
));
490 if (ret
!= sizeof(infolen
)) {
491 DEBUG(10,("cups_async_callback: infolen read failed %s\n",
496 DEBUG(11,("cups_async_callback: read infolen %u\n",
497 (unsigned int)infolen
));
500 name
= TALLOC_ARRAY(frame
, char, namelen
);
504 ret
= sys_read(fd
, name
, namelen
);
509 if (ret
!= namelen
) {
510 DEBUG(10,("cups_async_callback: name read failed %s\n",
514 DEBUG(11,("cups_async_callback: read name %s\n",
520 info
= TALLOC_ARRAY(frame
, char, infolen
);
524 ret
= sys_read(fd
, info
, infolen
);
529 if (ret
!= infolen
) {
530 DEBUG(10,("cups_async_callback: info read failed %s\n",
534 DEBUG(11,("cups_async_callback: read info %s\n",
540 /* Add to our local pcap cache. */
541 pcap_cache_add_specific(&tmp_pcap_cache
, name
, info
);
547 if (tmp_pcap_cache
) {
548 /* We got a namelist, replace our local cache. */
549 pcap_cache_destroy_specific(&local_pcap_copy
);
550 local_pcap_copy
= tmp_pcap_cache
;
552 /* And the systemwide pcap cache. */
553 pcap_cache_replace(local_pcap_copy
);
555 /* Caller may have requested post cache fill callback */
556 if (cb_args
->post_cache_fill_fn
) {
557 cb_args
->post_cache_fill_fn();
560 DEBUG(2,("cups_async_callback: failed to read a new "
564 TALLOC_FREE(cb_args
);
565 TALLOC_FREE(cache_fd_event
);
568 bool cups_cache_reload(void (*post_cache_fill_fn
)(void))
570 struct cups_async_cb_args
*cb_args
;
573 cb_args
= TALLOC_P(NULL
, struct cups_async_cb_args
);
577 cb_args
->post_cache_fill_fn
= post_cache_fill_fn
;
578 p_pipe_fd
= &cb_args
->pipe_fd
;
581 /* Set up an async refresh. */
582 if (!cups_pcap_load_async(p_pipe_fd
)) {
583 talloc_free(cb_args
);
586 if (!local_pcap_copy
) {
587 /* We have no local cache, wait directly for
588 * async refresh to complete.
590 DEBUG(10,("cups_cache_reload: sync read on fd %d\n",
593 cups_async_callback(smbd_event_context(),
597 if (!local_pcap_copy
) {
601 /* Replace the system cache with our
603 pcap_cache_replace(local_pcap_copy
);
605 DEBUG(10,("cups_cache_reload: async read on fd %d\n",
608 /* Trigger an event when the pipe can be read. */
609 cache_fd_event
= event_add_fd(smbd_event_context(),
614 if (!cache_fd_event
) {
616 talloc_free(cb_args
);
624 * 'cups_job_delete()' - Delete a job.
627 static int cups_job_delete(const char *sharename
, const char *lprm_command
, struct printjob
*pjob
)
629 TALLOC_CTX
*frame
= talloc_stackframe();
630 int ret
= 1; /* Return value */
631 http_t
*http
= NULL
; /* HTTP connection to server */
632 ipp_t
*request
= NULL
, /* IPP Request */
633 *response
= NULL
; /* IPP Response */
634 cups_lang_t
*language
= NULL
; /* Default language */
636 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
639 DEBUG(5,("cups_job_delete(%s, %p (%d))\n", sharename
, pjob
, pjob
->sysjob
));
642 * Make sure we don't ask for passwords...
645 cupsSetPasswordCB(cups_passwd_cb
);
648 * Try to connect to the server...
651 if ((http
= cups_connect(frame
)) == NULL
) {
656 * Build an IPP_CANCEL_JOB request, which requires the following
660 * attributes-natural-language
662 * requesting-user-name
667 request
->request
.op
.operation_id
= IPP_CANCEL_JOB
;
668 request
->request
.op
.request_id
= 1;
670 language
= cupsLangDefault();
672 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
673 "attributes-charset", NULL
, "utf-8");
675 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
676 "attributes-natural-language", NULL
, language
->language
);
678 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/jobs/%d", pjob
->sysjob
);
680 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
682 if (!push_utf8_talloc(frame
, &user
, pjob
->user
, &size
)) {
686 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
690 * Do the request and get back a response...
693 if ((response
= cupsDoRequest(http
, request
, "/jobs")) != NULL
) {
694 if (response
->request
.status
.status_code
>= IPP_OK_CONFLICT
) {
695 DEBUG(0,("Unable to cancel job %d - %s\n", pjob
->sysjob
,
696 ippErrorString(cupsLastError())));
701 DEBUG(0,("Unable to cancel job %d - %s\n", pjob
->sysjob
,
702 ippErrorString(cupsLastError())));
710 cupsLangFree(language
);
721 * 'cups_job_pause()' - Pause a job.
724 static int cups_job_pause(int snum
, struct printjob
*pjob
)
726 TALLOC_CTX
*frame
= talloc_stackframe();
727 int ret
= 1; /* Return value */
728 http_t
*http
= NULL
; /* HTTP connection to server */
729 ipp_t
*request
= NULL
, /* IPP Request */
730 *response
= NULL
; /* IPP Response */
731 cups_lang_t
*language
= NULL
; /* Default language */
733 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
736 DEBUG(5,("cups_job_pause(%d, %p (%d))\n", snum
, pjob
, pjob
->sysjob
));
739 * Make sure we don't ask for passwords...
742 cupsSetPasswordCB(cups_passwd_cb
);
745 * Try to connect to the server...
748 if ((http
= cups_connect(frame
)) == NULL
) {
753 * Build an IPP_HOLD_JOB request, which requires the following
757 * attributes-natural-language
759 * requesting-user-name
764 request
->request
.op
.operation_id
= IPP_HOLD_JOB
;
765 request
->request
.op
.request_id
= 1;
767 language
= cupsLangDefault();
769 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
770 "attributes-charset", NULL
, "utf-8");
772 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
773 "attributes-natural-language", NULL
, language
->language
);
775 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/jobs/%d", pjob
->sysjob
);
777 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
779 if (!push_utf8_talloc(frame
, &user
, pjob
->user
, &size
)) {
782 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
786 * Do the request and get back a response...
789 if ((response
= cupsDoRequest(http
, request
, "/jobs")) != NULL
) {
790 if (response
->request
.status
.status_code
>= IPP_OK_CONFLICT
) {
791 DEBUG(0,("Unable to hold job %d - %s\n", pjob
->sysjob
,
792 ippErrorString(cupsLastError())));
797 DEBUG(0,("Unable to hold job %d - %s\n", pjob
->sysjob
,
798 ippErrorString(cupsLastError())));
806 cupsLangFree(language
);
817 * 'cups_job_resume()' - Resume a paused job.
820 static int cups_job_resume(int snum
, struct printjob
*pjob
)
822 TALLOC_CTX
*frame
= talloc_stackframe();
823 int ret
= 1; /* Return value */
824 http_t
*http
= NULL
; /* HTTP connection to server */
825 ipp_t
*request
= NULL
, /* IPP Request */
826 *response
= NULL
; /* IPP Response */
827 cups_lang_t
*language
= NULL
; /* Default language */
829 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
832 DEBUG(5,("cups_job_resume(%d, %p (%d))\n", snum
, pjob
, pjob
->sysjob
));
835 * Make sure we don't ask for passwords...
838 cupsSetPasswordCB(cups_passwd_cb
);
841 * Try to connect to the server...
844 if ((http
= cups_connect(frame
)) == NULL
) {
849 * Build an IPP_RELEASE_JOB request, which requires the following
853 * attributes-natural-language
855 * requesting-user-name
860 request
->request
.op
.operation_id
= IPP_RELEASE_JOB
;
861 request
->request
.op
.request_id
= 1;
863 language
= cupsLangDefault();
865 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
866 "attributes-charset", NULL
, "utf-8");
868 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
869 "attributes-natural-language", NULL
, language
->language
);
871 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/jobs/%d", pjob
->sysjob
);
873 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
875 if (!push_utf8_talloc(frame
, &user
, pjob
->user
, &size
)) {
878 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
882 * Do the request and get back a response...
885 if ((response
= cupsDoRequest(http
, request
, "/jobs")) != NULL
) {
886 if (response
->request
.status
.status_code
>= IPP_OK_CONFLICT
) {
887 DEBUG(0,("Unable to release job %d - %s\n", pjob
->sysjob
,
888 ippErrorString(cupsLastError())));
893 DEBUG(0,("Unable to release job %d - %s\n", pjob
->sysjob
,
894 ippErrorString(cupsLastError())));
902 cupsLangFree(language
);
913 * 'cups_job_submit()' - Submit a job for printing.
916 static int cups_job_submit(int snum
, struct printjob
*pjob
)
918 TALLOC_CTX
*frame
= talloc_stackframe();
919 int ret
= 1; /* Return value */
920 http_t
*http
= NULL
; /* HTTP connection to server */
921 ipp_t
*request
= NULL
, /* IPP Request */
922 *response
= NULL
; /* IPP Response */
923 ipp_attribute_t
*attr_job_id
= NULL
; /* IPP Attribute "job-id" */
924 cups_lang_t
*language
= NULL
; /* Default language */
925 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
926 const char *clientname
= NULL
; /* hostname of client for job-originating-host attribute */
927 char *new_jobname
= NULL
;
929 cups_option_t
*options
= NULL
;
930 char *printername
= NULL
;
932 char *jobname
= NULL
;
933 char *cupsoptions
= NULL
;
934 char *filename
= NULL
;
936 uint32_t jobid
= (uint32_t)-1;
937 char addr
[INET6_ADDRSTRLEN
];
939 DEBUG(5,("cups_job_submit(%d, %p)\n", snum
, pjob
));
942 * Make sure we don't ask for passwords...
945 cupsSetPasswordCB(cups_passwd_cb
);
948 * Try to connect to the server...
951 if ((http
= cups_connect(frame
)) == NULL
) {
956 * Build an IPP_PRINT_JOB request, which requires the following
960 * attributes-natural-language
962 * requesting-user-name
968 request
->request
.op
.operation_id
= IPP_PRINT_JOB
;
969 request
->request
.op
.request_id
= 1;
971 language
= cupsLangDefault();
973 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
974 "attributes-charset", NULL
, "utf-8");
976 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
977 "attributes-natural-language", NULL
, language
->language
);
979 if (!push_utf8_talloc(frame
, &printername
, PRINTERNAME(snum
), &size
)) {
982 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/printers/%s",
985 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
986 "printer-uri", NULL
, uri
);
988 if (!push_utf8_talloc(frame
, &user
, pjob
->user
, &size
)) {
991 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
994 clientname
= client_name(get_client_fd());
995 if (strcmp(clientname
, "UNKNOWN") == 0) {
996 clientname
= client_addr(get_client_fd(),addr
,sizeof(addr
));
999 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1000 "job-originating-host-name", NULL
,
1003 /* Get the jobid from the filename. */
1004 jobid
= print_parse_jobid(pjob
->filename
);
1005 if (jobid
== (uint32_t)-1) {
1006 DEBUG(0,("cups_job_submit: failed to parse jobid from name %s\n",
1011 if (!push_utf8_talloc(frame
, &jobname
, pjob
->jobname
, &size
)) {
1014 new_jobname
= talloc_asprintf(frame
,
1015 "%s%.8u %s", PRINT_SPOOL_PREFIX
,
1016 (unsigned int)jobid
,
1018 if (new_jobname
== NULL
) {
1022 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name", NULL
,
1026 * add any options defined in smb.conf
1029 if (!push_utf8_talloc(frame
, &cupsoptions
, lp_cups_options(snum
), &size
)) {
1034 num_options
= cupsParseOptions(cupsoptions
, num_options
, &options
);
1037 cupsEncodeOptions(request
, num_options
, options
);
1040 * Do the request and get back a response...
1043 slprintf(uri
, sizeof(uri
) - 1, "/printers/%s", printername
);
1045 if (!push_utf8_talloc(frame
, &filename
, pjob
->filename
, &size
)) {
1048 if ((response
= cupsDoFileRequest(http
, request
, uri
, pjob
->filename
)) != NULL
) {
1049 if (response
->request
.status
.status_code
>= IPP_OK_CONFLICT
) {
1050 DEBUG(0,("Unable to print file to %s - %s\n", PRINTERNAME(snum
),
1051 ippErrorString(cupsLastError())));
1054 attr_job_id
= ippFindAttribute(response
, "job-id", IPP_TAG_INTEGER
);
1056 pjob
->sysjob
= attr_job_id
->values
[0].integer
;
1057 DEBUG(5,("cups_job_submit: job-id %d\n", pjob
->sysjob
));
1059 DEBUG(0,("Missing job-id attribute in IPP response"));
1063 DEBUG(0,("Unable to print file to `%s' - %s\n", PRINTERNAME(snum
),
1064 ippErrorString(cupsLastError())));
1068 unlink(pjob
->filename
);
1069 /* else print_job_end will do it for us */
1073 ippDelete(response
);
1076 cupsLangFree(language
);
1087 * 'cups_queue_get()' - Get all the jobs in the print queue.
1090 static int cups_queue_get(const char *sharename
,
1091 enum printing_types printing_type
,
1093 print_queue_struct
**q
,
1094 print_status_struct
*status
)
1096 TALLOC_CTX
*frame
= talloc_stackframe();
1097 char *printername
= NULL
;
1098 http_t
*http
= NULL
; /* HTTP connection to server */
1099 ipp_t
*request
= NULL
, /* IPP Request */
1100 *response
= NULL
; /* IPP Response */
1101 ipp_attribute_t
*attr
= NULL
; /* Current attribute */
1102 cups_lang_t
*language
= NULL
; /* Default language */
1103 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
1104 int qcount
= 0, /* Number of active queue entries */
1105 qalloc
= 0; /* Number of queue entries allocated */
1106 print_queue_struct
*queue
= NULL
, /* Queue entries */
1107 *temp
; /* Temporary pointer for queue */
1108 char *user_name
= NULL
, /* job-originating-user-name attribute */
1109 *job_name
= NULL
; /* job-name attribute */
1110 int job_id
; /* job-id attribute */
1111 int job_k_octets
; /* job-k-octets attribute */
1112 time_t job_time
; /* time-at-creation attribute */
1113 ipp_jstate_t job_status
; /* job-status attribute */
1114 int job_priority
; /* job-priority attribute */
1116 static const char *jattrs
[] = /* Requested job attributes */
1121 "job-originating-user-name",
1126 static const char *pattrs
[] = /* Requested printer attributes */
1129 "printer-state-message"
1134 /* HACK ALERT!!! The problem with support the 'printer name'
1135 option is that we key the tdb off the sharename. So we will
1136 overload the lpq_command string to pass in the printername
1137 (which is basically what we do for non-cups printers ... using
1138 the lpq_command to get the queue listing). */
1140 if (!push_utf8_talloc(frame
, &printername
, lpq_command
, &size
)) {
1143 DEBUG(5,("cups_queue_get(%s, %p, %p)\n", lpq_command
, q
, status
));
1146 * Make sure we don't ask for passwords...
1149 cupsSetPasswordCB(cups_passwd_cb
);
1152 * Try to connect to the server...
1155 if ((http
= cups_connect(frame
)) == NULL
) {
1160 * Generate the printer URI...
1163 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/printers/%s", printername
);
1166 * Build an IPP_GET_JOBS request, which requires the following
1169 * attributes-charset
1170 * attributes-natural-language
1171 * requested-attributes
1177 request
->request
.op
.operation_id
= IPP_GET_JOBS
;
1178 request
->request
.op
.request_id
= 1;
1180 language
= cupsLangDefault();
1182 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1183 "attributes-charset", NULL
, "utf-8");
1185 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1186 "attributes-natural-language", NULL
, language
->language
);
1188 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1189 "requested-attributes",
1190 (sizeof(jattrs
) / sizeof(jattrs
[0])),
1193 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1194 "printer-uri", NULL
, uri
);
1197 * Do the request and get back a response...
1200 if ((response
= cupsDoRequest(http
, request
, "/")) == NULL
) {
1201 DEBUG(0,("Unable to get jobs for %s - %s\n", uri
,
1202 ippErrorString(cupsLastError())));
1206 if (response
->request
.status
.status_code
>= IPP_OK_CONFLICT
) {
1207 DEBUG(0,("Unable to get jobs for %s - %s\n", uri
,
1208 ippErrorString(response
->request
.status
.status_code
)));
1213 * Process the jobs...
1220 for (attr
= response
->attrs
; attr
!= NULL
; attr
= attr
->next
) {
1222 * Skip leading attributes until we hit a job...
1225 while (attr
!= NULL
&& attr
->group_tag
!= IPP_TAG_JOB
)
1232 * Allocate memory as needed...
1234 if (qcount
>= qalloc
) {
1237 queue
= SMB_REALLOC_ARRAY(queue
, print_queue_struct
, qalloc
);
1239 if (queue
== NULL
) {
1240 DEBUG(0,("cups_queue_get: Not enough memory!"));
1246 temp
= queue
+ qcount
;
1247 memset(temp
, 0, sizeof(print_queue_struct
));
1250 * Pull the needed attributes from this job...
1255 job_status
= IPP_JOB_PENDING
;
1261 while (attr
!= NULL
&& attr
->group_tag
== IPP_TAG_JOB
) {
1262 if (attr
->name
== NULL
) {
1267 if (strcmp(attr
->name
, "job-id") == 0 &&
1268 attr
->value_tag
== IPP_TAG_INTEGER
)
1269 job_id
= attr
->values
[0].integer
;
1271 if (strcmp(attr
->name
, "job-k-octets") == 0 &&
1272 attr
->value_tag
== IPP_TAG_INTEGER
)
1273 job_k_octets
= attr
->values
[0].integer
;
1275 if (strcmp(attr
->name
, "job-priority") == 0 &&
1276 attr
->value_tag
== IPP_TAG_INTEGER
)
1277 job_priority
= attr
->values
[0].integer
;
1279 if (strcmp(attr
->name
, "job-state") == 0 &&
1280 attr
->value_tag
== IPP_TAG_ENUM
)
1281 job_status
= (ipp_jstate_t
)(attr
->values
[0].integer
);
1283 if (strcmp(attr
->name
, "time-at-creation") == 0 &&
1284 attr
->value_tag
== IPP_TAG_INTEGER
)
1285 job_time
= attr
->values
[0].integer
;
1287 if (strcmp(attr
->name
, "job-name") == 0 &&
1288 attr
->value_tag
== IPP_TAG_NAME
) {
1289 if (!pull_utf8_talloc(frame
,
1291 attr
->values
[0].string
.text
,
1297 if (strcmp(attr
->name
, "job-originating-user-name") == 0 &&
1298 attr
->value_tag
== IPP_TAG_NAME
) {
1299 if (!pull_utf8_talloc(frame
,
1301 attr
->values
[0].string
.text
,
1311 * See if we have everything needed...
1314 if (user_name
== NULL
|| job_name
== NULL
|| job_id
== 0) {
1322 temp
->size
= job_k_octets
* 1024;
1323 temp
->status
= job_status
== IPP_JOB_PENDING
? LPQ_QUEUED
:
1324 job_status
== IPP_JOB_STOPPED
? LPQ_PAUSED
:
1325 job_status
== IPP_JOB_HELD
? LPQ_PAUSED
:
1327 temp
->priority
= job_priority
;
1328 temp
->time
= job_time
;
1329 strlcpy(temp
->fs_user
, user_name
, sizeof(temp
->fs_user
));
1330 strlcpy(temp
->fs_file
, job_name
, sizeof(temp
->fs_file
));
1338 ippDelete(response
);
1342 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the
1343 * following attributes:
1345 * attributes-charset
1346 * attributes-natural-language
1347 * requested-attributes
1353 request
->request
.op
.operation_id
= IPP_GET_PRINTER_ATTRIBUTES
;
1354 request
->request
.op
.request_id
= 1;
1356 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1357 "attributes-charset", NULL
, "utf-8");
1359 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1360 "attributes-natural-language", NULL
, language
->language
);
1362 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1363 "requested-attributes",
1364 (sizeof(pattrs
) / sizeof(pattrs
[0])),
1367 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1368 "printer-uri", NULL
, uri
);
1371 * Do the request and get back a response...
1374 if ((response
= cupsDoRequest(http
, request
, "/")) == NULL
) {
1375 DEBUG(0,("Unable to get printer status for %s - %s\n", printername
,
1376 ippErrorString(cupsLastError())));
1380 if (response
->request
.status
.status_code
>= IPP_OK_CONFLICT
) {
1381 DEBUG(0,("Unable to get printer status for %s - %s\n", printername
,
1382 ippErrorString(response
->request
.status
.status_code
)));
1387 * Get the current printer status and convert it to the SAMBA values.
1390 if ((attr
= ippFindAttribute(response
, "printer-state", IPP_TAG_ENUM
)) != NULL
) {
1391 if (attr
->values
[0].integer
== IPP_PRINTER_STOPPED
)
1392 status
->status
= LPSTAT_STOPPED
;
1394 status
->status
= LPSTAT_OK
;
1397 if ((attr
= ippFindAttribute(response
, "printer-state-message",
1398 IPP_TAG_TEXT
)) != NULL
) {
1400 if (!pull_utf8_talloc(frame
, &msg
,
1401 attr
->values
[0].string
.text
,
1407 fstrcpy(status
->message
, msg
);
1413 * Return the job queue...
1419 ippDelete(response
);
1422 cupsLangFree(language
);
1433 * 'cups_queue_pause()' - Pause a print queue.
1436 static int cups_queue_pause(int snum
)
1438 TALLOC_CTX
*frame
= talloc_stackframe();
1439 int ret
= 1; /* Return value */
1440 http_t
*http
= NULL
; /* HTTP connection to server */
1441 ipp_t
*request
= NULL
, /* IPP Request */
1442 *response
= NULL
; /* IPP Response */
1443 cups_lang_t
*language
= NULL
; /* Default language */
1444 char *printername
= NULL
;
1445 char *username
= NULL
;
1446 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
1449 DEBUG(5,("cups_queue_pause(%d)\n", snum
));
1452 * Make sure we don't ask for passwords...
1455 cupsSetPasswordCB(cups_passwd_cb
);
1458 * Try to connect to the server...
1461 if ((http
= cups_connect(frame
)) == NULL
) {
1466 * Build an IPP_PAUSE_PRINTER request, which requires the following
1469 * attributes-charset
1470 * attributes-natural-language
1472 * requesting-user-name
1477 request
->request
.op
.operation_id
= IPP_PAUSE_PRINTER
;
1478 request
->request
.op
.request_id
= 1;
1480 language
= cupsLangDefault();
1482 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1483 "attributes-charset", NULL
, "utf-8");
1485 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1486 "attributes-natural-language", NULL
, language
->language
);
1488 if (!push_utf8_talloc(frame
, &printername
, PRINTERNAME(snum
), &size
)) {
1491 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/printers/%s",
1494 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
1496 if (!push_utf8_talloc(frame
, &username
, current_user_info
.unix_name
, &size
)) {
1499 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
1503 * Do the request and get back a response...
1506 if ((response
= cupsDoRequest(http
, request
, "/admin/")) != NULL
) {
1507 if (response
->request
.status
.status_code
>= IPP_OK_CONFLICT
) {
1508 DEBUG(0,("Unable to pause printer %s - %s\n", PRINTERNAME(snum
),
1509 ippErrorString(cupsLastError())));
1514 DEBUG(0,("Unable to pause printer %s - %s\n", PRINTERNAME(snum
),
1515 ippErrorString(cupsLastError())));
1520 ippDelete(response
);
1523 cupsLangFree(language
);
1534 * 'cups_queue_resume()' - Restart a print queue.
1537 static int cups_queue_resume(int snum
)
1539 TALLOC_CTX
*frame
= talloc_stackframe();
1540 int ret
= 1; /* Return value */
1541 http_t
*http
= NULL
; /* HTTP connection to server */
1542 ipp_t
*request
= NULL
, /* IPP Request */
1543 *response
= NULL
; /* IPP Response */
1544 cups_lang_t
*language
= NULL
; /* Default language */
1545 char *printername
= NULL
;
1546 char *username
= NULL
;
1547 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
1550 DEBUG(5,("cups_queue_resume(%d)\n", snum
));
1553 * Make sure we don't ask for passwords...
1556 cupsSetPasswordCB(cups_passwd_cb
);
1559 * Try to connect to the server...
1562 if ((http
= cups_connect(frame
)) == NULL
) {
1567 * Build an IPP_RESUME_PRINTER request, which requires the following
1570 * attributes-charset
1571 * attributes-natural-language
1573 * requesting-user-name
1578 request
->request
.op
.operation_id
= IPP_RESUME_PRINTER
;
1579 request
->request
.op
.request_id
= 1;
1581 language
= cupsLangDefault();
1583 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1584 "attributes-charset", NULL
, "utf-8");
1586 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1587 "attributes-natural-language", NULL
, language
->language
);
1589 if (!push_utf8_talloc(frame
, &printername
, PRINTERNAME(snum
), &size
)) {
1592 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/printers/%s",
1595 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
1597 if (!push_utf8_talloc(frame
, &username
, current_user_info
.unix_name
, &size
)) {
1600 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
1604 * Do the request and get back a response...
1607 if ((response
= cupsDoRequest(http
, request
, "/admin/")) != NULL
) {
1608 if (response
->request
.status
.status_code
>= IPP_OK_CONFLICT
) {
1609 DEBUG(0,("Unable to resume printer %s - %s\n", PRINTERNAME(snum
),
1610 ippErrorString(cupsLastError())));
1615 DEBUG(0,("Unable to resume printer %s - %s\n", PRINTERNAME(snum
),
1616 ippErrorString(cupsLastError())));
1621 ippDelete(response
);
1624 cupsLangFree(language
);
1633 /*******************************************************************
1634 * CUPS printing interface definitions...
1635 ******************************************************************/
1637 struct printif cups_printif
=
1649 bool cups_pull_comment_location(NT_PRINTER_INFO_LEVEL_2
*printer
)
1651 TALLOC_CTX
*frame
= talloc_stackframe();
1652 http_t
*http
= NULL
; /* HTTP connection to server */
1653 ipp_t
*request
= NULL
, /* IPP Request */
1654 *response
= NULL
; /* IPP Response */
1655 ipp_attribute_t
*attr
; /* Current attribute */
1656 cups_lang_t
*language
= NULL
; /* Default language */
1657 char uri
[HTTP_MAX_URI
];
1658 char *server
= NULL
;
1659 char *sharename
= NULL
;
1661 static const char *requested
[] =/* Requested attributes */
1670 DEBUG(5, ("pulling %s location\n", printer
->sharename
));
1673 * Make sure we don't ask for passwords...
1676 cupsSetPasswordCB(cups_passwd_cb
);
1679 * Try to connect to the server...
1682 if ((http
= cups_connect(frame
)) == NULL
) {
1688 request
->request
.op
.operation_id
= IPP_GET_PRINTER_ATTRIBUTES
;
1689 request
->request
.op
.request_id
= 1;
1691 language
= cupsLangDefault();
1693 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1694 "attributes-charset", NULL
, "utf-8");
1696 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1697 "attributes-natural-language", NULL
, language
->language
);
1699 if (lp_cups_server() != NULL
&& strlen(lp_cups_server()) > 0) {
1700 if (!push_utf8_talloc(frame
, &server
, lp_cups_server(), &size
)) {
1704 server
= talloc_strdup(frame
,cupsServer());
1709 if (!push_utf8_talloc(frame
, &sharename
, printer
->sharename
, &size
)) {
1712 slprintf(uri
, sizeof(uri
) - 1, "ipp://%s/printers/%s",
1715 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1716 "printer-uri", NULL
, uri
);
1718 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1719 "requested-attributes",
1720 (sizeof(requested
) / sizeof(requested
[0])),
1724 * Do the request and get back a response...
1727 if ((response
= cupsDoRequest(http
, request
, "/")) == NULL
) {
1728 DEBUG(0,("Unable to get printer attributes - %s\n",
1729 ippErrorString(cupsLastError())));
1733 for (attr
= response
->attrs
; attr
!= NULL
;) {
1735 * Skip leading attributes until we hit a printer...
1738 while (attr
!= NULL
&& attr
->group_tag
!= IPP_TAG_PRINTER
)
1745 * Pull the needed attributes from this printer...
1748 while ( attr
&& (attr
->group_tag
== IPP_TAG_PRINTER
) ) {
1749 if (strcmp(attr
->name
, "printer-name") == 0 &&
1750 attr
->value_tag
== IPP_TAG_NAME
) {
1751 if (!pull_utf8_talloc(frame
,
1753 attr
->values
[0].string
.text
,
1759 /* Grab the comment if we don't have one */
1760 if ( (strcmp(attr
->name
, "printer-info") == 0)
1761 && (attr
->value_tag
== IPP_TAG_TEXT
)
1762 && !strlen(printer
->comment
) )
1764 char *comment
= NULL
;
1765 if (!pull_utf8_talloc(frame
,
1767 attr
->values
[0].string
.text
,
1771 DEBUG(5,("cups_pull_comment_location: Using cups comment: %s\n",
1773 strlcpy(printer
->comment
,
1775 sizeof(printer
->comment
));
1778 /* Grab the location if we don't have one */
1779 if ( (strcmp(attr
->name
, "printer-location") == 0)
1780 && (attr
->value_tag
== IPP_TAG_TEXT
)
1781 && !strlen(printer
->location
) )
1783 char *location
= NULL
;
1784 if (!pull_utf8_talloc(frame
,
1786 attr
->values
[0].string
.text
,
1790 DEBUG(5,("cups_pull_comment_location: Using cups location: %s\n",
1792 strlcpy(printer
->location
,
1794 sizeof(printer
->location
));
1801 * We have everything needed...
1812 ippDelete(response
);
1819 cupsLangFree(language
);
1829 /* this keeps fussy compilers happy */
1830 void print_cups_dummy(void);
1831 void print_cups_dummy(void) {}
1832 #endif /* HAVE_CUPS */