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 static void cups_async_callback(struct event_context
*event_ctx
,
449 struct fd_event
*event
,
453 TALLOC_CTX
*frame
= talloc_stackframe();
455 struct pcap_cache
*tmp_pcap_cache
= NULL
;
457 DEBUG(5,("cups_async_callback: callback received for printer data. "
461 char *name
= NULL
, *info
= NULL
;
462 size_t namelen
= 0, infolen
= 0;
465 ret
= sys_read(fd
, &namelen
, sizeof(namelen
));
470 if (ret
!= sizeof(namelen
)) {
471 DEBUG(10,("cups_async_callback: namelen read failed %d %s\n",
472 errno
, strerror(errno
)));
476 DEBUG(11,("cups_async_callback: read namelen %u\n",
477 (unsigned int)namelen
));
479 ret
= sys_read(fd
, &infolen
, sizeof(infolen
));
484 if (ret
!= sizeof(infolen
)) {
485 DEBUG(10,("cups_async_callback: infolen read failed %s\n",
490 DEBUG(11,("cups_async_callback: read infolen %u\n",
491 (unsigned int)infolen
));
494 name
= TALLOC_ARRAY(frame
, char, namelen
);
498 ret
= sys_read(fd
, name
, namelen
);
503 if (ret
!= namelen
) {
504 DEBUG(10,("cups_async_callback: name read failed %s\n",
508 DEBUG(11,("cups_async_callback: read name %s\n",
514 info
= TALLOC_ARRAY(frame
, char, infolen
);
518 ret
= sys_read(fd
, info
, infolen
);
523 if (ret
!= infolen
) {
524 DEBUG(10,("cups_async_callback: info read failed %s\n",
528 DEBUG(11,("cups_async_callback: read info %s\n",
534 /* Add to our local pcap cache. */
535 pcap_cache_add_specific(&tmp_pcap_cache
, name
, info
);
541 if (tmp_pcap_cache
) {
542 /* We got a namelist, replace our local cache. */
543 pcap_cache_destroy_specific(&local_pcap_copy
);
544 local_pcap_copy
= tmp_pcap_cache
;
546 /* And the systemwide pcap cache. */
547 pcap_cache_replace(local_pcap_copy
);
549 DEBUG(2,("cups_async_callback: failed to read a new "
554 TALLOC_FREE(cache_fd_event
);
557 bool cups_cache_reload(void)
559 int *p_pipe_fd
= TALLOC_P(NULL
, int);
567 /* Set up an async refresh. */
568 if (!cups_pcap_load_async(p_pipe_fd
)) {
571 if (!local_pcap_copy
) {
572 /* We have no local cache, wait directly for
573 * async refresh to complete.
575 DEBUG(10,("cups_cache_reload: sync read on fd %d\n",
578 cups_async_callback(smbd_event_context(),
582 if (!local_pcap_copy
) {
586 /* Replace the system cache with our
588 pcap_cache_replace(local_pcap_copy
);
590 DEBUG(10,("cups_cache_reload: async read on fd %d\n",
593 /* Trigger an event when the pipe can be read. */
594 cache_fd_event
= event_add_fd(smbd_event_context(),
599 if (!cache_fd_event
) {
601 TALLOC_FREE(p_pipe_fd
);
609 * 'cups_job_delete()' - Delete a job.
612 static int cups_job_delete(const char *sharename
, const char *lprm_command
, struct printjob
*pjob
)
614 TALLOC_CTX
*frame
= talloc_stackframe();
615 int ret
= 1; /* Return value */
616 http_t
*http
= NULL
; /* HTTP connection to server */
617 ipp_t
*request
= NULL
, /* IPP Request */
618 *response
= NULL
; /* IPP Response */
619 cups_lang_t
*language
= NULL
; /* Default language */
621 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
624 DEBUG(5,("cups_job_delete(%s, %p (%d))\n", sharename
, pjob
, pjob
->sysjob
));
627 * Make sure we don't ask for passwords...
630 cupsSetPasswordCB(cups_passwd_cb
);
633 * Try to connect to the server...
636 if ((http
= cups_connect(frame
)) == NULL
) {
641 * Build an IPP_CANCEL_JOB request, which requires the following
645 * attributes-natural-language
647 * requesting-user-name
652 request
->request
.op
.operation_id
= IPP_CANCEL_JOB
;
653 request
->request
.op
.request_id
= 1;
655 language
= cupsLangDefault();
657 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
658 "attributes-charset", NULL
, "utf-8");
660 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
661 "attributes-natural-language", NULL
, language
->language
);
663 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/jobs/%d", pjob
->sysjob
);
665 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
667 if (!push_utf8_talloc(frame
, &user
, pjob
->user
, &size
)) {
671 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
675 * Do the request and get back a response...
678 if ((response
= cupsDoRequest(http
, request
, "/jobs")) != NULL
) {
679 if (response
->request
.status
.status_code
>= IPP_OK_CONFLICT
) {
680 DEBUG(0,("Unable to cancel job %d - %s\n", pjob
->sysjob
,
681 ippErrorString(cupsLastError())));
686 DEBUG(0,("Unable to cancel job %d - %s\n", pjob
->sysjob
,
687 ippErrorString(cupsLastError())));
695 cupsLangFree(language
);
706 * 'cups_job_pause()' - Pause a job.
709 static int cups_job_pause(int snum
, struct printjob
*pjob
)
711 TALLOC_CTX
*frame
= talloc_stackframe();
712 int ret
= 1; /* Return value */
713 http_t
*http
= NULL
; /* HTTP connection to server */
714 ipp_t
*request
= NULL
, /* IPP Request */
715 *response
= NULL
; /* IPP Response */
716 cups_lang_t
*language
= NULL
; /* Default language */
718 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
721 DEBUG(5,("cups_job_pause(%d, %p (%d))\n", snum
, pjob
, pjob
->sysjob
));
724 * Make sure we don't ask for passwords...
727 cupsSetPasswordCB(cups_passwd_cb
);
730 * Try to connect to the server...
733 if ((http
= cups_connect(frame
)) == NULL
) {
738 * Build an IPP_HOLD_JOB request, which requires the following
742 * attributes-natural-language
744 * requesting-user-name
749 request
->request
.op
.operation_id
= IPP_HOLD_JOB
;
750 request
->request
.op
.request_id
= 1;
752 language
= cupsLangDefault();
754 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
755 "attributes-charset", NULL
, "utf-8");
757 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
758 "attributes-natural-language", NULL
, language
->language
);
760 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/jobs/%d", pjob
->sysjob
);
762 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
764 if (!push_utf8_talloc(frame
, &user
, pjob
->user
, &size
)) {
767 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
771 * Do the request and get back a response...
774 if ((response
= cupsDoRequest(http
, request
, "/jobs")) != NULL
) {
775 if (response
->request
.status
.status_code
>= IPP_OK_CONFLICT
) {
776 DEBUG(0,("Unable to hold job %d - %s\n", pjob
->sysjob
,
777 ippErrorString(cupsLastError())));
782 DEBUG(0,("Unable to hold job %d - %s\n", pjob
->sysjob
,
783 ippErrorString(cupsLastError())));
791 cupsLangFree(language
);
802 * 'cups_job_resume()' - Resume a paused job.
805 static int cups_job_resume(int snum
, struct printjob
*pjob
)
807 TALLOC_CTX
*frame
= talloc_stackframe();
808 int ret
= 1; /* Return value */
809 http_t
*http
= NULL
; /* HTTP connection to server */
810 ipp_t
*request
= NULL
, /* IPP Request */
811 *response
= NULL
; /* IPP Response */
812 cups_lang_t
*language
= NULL
; /* Default language */
814 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
817 DEBUG(5,("cups_job_resume(%d, %p (%d))\n", snum
, pjob
, pjob
->sysjob
));
820 * Make sure we don't ask for passwords...
823 cupsSetPasswordCB(cups_passwd_cb
);
826 * Try to connect to the server...
829 if ((http
= cups_connect(frame
)) == NULL
) {
834 * Build an IPP_RELEASE_JOB request, which requires the following
838 * attributes-natural-language
840 * requesting-user-name
845 request
->request
.op
.operation_id
= IPP_RELEASE_JOB
;
846 request
->request
.op
.request_id
= 1;
848 language
= cupsLangDefault();
850 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
851 "attributes-charset", NULL
, "utf-8");
853 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
854 "attributes-natural-language", NULL
, language
->language
);
856 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/jobs/%d", pjob
->sysjob
);
858 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
860 if (!push_utf8_talloc(frame
, &user
, pjob
->user
, &size
)) {
863 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
867 * Do the request and get back a response...
870 if ((response
= cupsDoRequest(http
, request
, "/jobs")) != NULL
) {
871 if (response
->request
.status
.status_code
>= IPP_OK_CONFLICT
) {
872 DEBUG(0,("Unable to release job %d - %s\n", pjob
->sysjob
,
873 ippErrorString(cupsLastError())));
878 DEBUG(0,("Unable to release job %d - %s\n", pjob
->sysjob
,
879 ippErrorString(cupsLastError())));
887 cupsLangFree(language
);
898 * 'cups_job_submit()' - Submit a job for printing.
901 static int cups_job_submit(int snum
, struct printjob
*pjob
)
903 TALLOC_CTX
*frame
= talloc_stackframe();
904 int ret
= 1; /* Return value */
905 http_t
*http
= NULL
; /* HTTP connection to server */
906 ipp_t
*request
= NULL
, /* IPP Request */
907 *response
= NULL
; /* IPP Response */
908 ipp_attribute_t
*attr_job_id
= NULL
; /* IPP Attribute "job-id" */
909 cups_lang_t
*language
= NULL
; /* Default language */
910 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
911 const char *clientname
= NULL
; /* hostname of client for job-originating-host attribute */
912 char *new_jobname
= NULL
;
914 cups_option_t
*options
= NULL
;
915 char *printername
= NULL
;
917 char *jobname
= NULL
;
918 char *cupsoptions
= NULL
;
919 char *filename
= NULL
;
921 uint32_t jobid
= (uint32_t)-1;
922 char addr
[INET6_ADDRSTRLEN
];
924 DEBUG(5,("cups_job_submit(%d, %p)\n", snum
, pjob
));
927 * Make sure we don't ask for passwords...
930 cupsSetPasswordCB(cups_passwd_cb
);
933 * Try to connect to the server...
936 if ((http
= cups_connect(frame
)) == NULL
) {
941 * Build an IPP_PRINT_JOB request, which requires the following
945 * attributes-natural-language
947 * requesting-user-name
953 request
->request
.op
.operation_id
= IPP_PRINT_JOB
;
954 request
->request
.op
.request_id
= 1;
956 language
= cupsLangDefault();
958 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
959 "attributes-charset", NULL
, "utf-8");
961 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
962 "attributes-natural-language", NULL
, language
->language
);
964 if (!push_utf8_talloc(frame
, &printername
, PRINTERNAME(snum
), &size
)) {
967 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/printers/%s",
970 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
971 "printer-uri", NULL
, uri
);
973 if (!push_utf8_talloc(frame
, &user
, pjob
->user
, &size
)) {
976 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
979 clientname
= client_name(get_client_fd());
980 if (strcmp(clientname
, "UNKNOWN") == 0) {
981 clientname
= client_addr(get_client_fd(),addr
,sizeof(addr
));
984 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
985 "job-originating-host-name", NULL
,
988 /* Get the jobid from the filename. */
989 jobid
= print_parse_jobid(pjob
->filename
);
990 if (jobid
== (uint32_t)-1) {
991 DEBUG(0,("cups_job_submit: failed to parse jobid from name %s\n",
996 if (!push_utf8_talloc(frame
, &jobname
, pjob
->jobname
, &size
)) {
999 new_jobname
= talloc_asprintf(frame
,
1000 "%s%.8u %s", PRINT_SPOOL_PREFIX
,
1001 (unsigned int)jobid
,
1003 if (new_jobname
== NULL
) {
1007 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name", NULL
,
1011 * add any options defined in smb.conf
1014 if (!push_utf8_talloc(frame
, &cupsoptions
, lp_cups_options(snum
), &size
)) {
1019 num_options
= cupsParseOptions(cupsoptions
, num_options
, &options
);
1022 cupsEncodeOptions(request
, num_options
, options
);
1025 * Do the request and get back a response...
1028 slprintf(uri
, sizeof(uri
) - 1, "/printers/%s", printername
);
1030 if (!push_utf8_talloc(frame
, &filename
, pjob
->filename
, &size
)) {
1033 if ((response
= cupsDoFileRequest(http
, request
, uri
, pjob
->filename
)) != NULL
) {
1034 if (response
->request
.status
.status_code
>= IPP_OK_CONFLICT
) {
1035 DEBUG(0,("Unable to print file to %s - %s\n", PRINTERNAME(snum
),
1036 ippErrorString(cupsLastError())));
1039 attr_job_id
= ippFindAttribute(response
, "job-id", IPP_TAG_INTEGER
);
1041 pjob
->sysjob
= attr_job_id
->values
[0].integer
;
1042 DEBUG(5,("cups_job_submit: job-id %d\n", pjob
->sysjob
));
1044 DEBUG(0,("Missing job-id attribute in IPP response"));
1048 DEBUG(0,("Unable to print file to `%s' - %s\n", PRINTERNAME(snum
),
1049 ippErrorString(cupsLastError())));
1053 unlink(pjob
->filename
);
1054 /* else print_job_end will do it for us */
1058 ippDelete(response
);
1061 cupsLangFree(language
);
1072 * 'cups_queue_get()' - Get all the jobs in the print queue.
1075 static int cups_queue_get(const char *sharename
,
1076 enum printing_types printing_type
,
1078 print_queue_struct
**q
,
1079 print_status_struct
*status
)
1081 TALLOC_CTX
*frame
= talloc_stackframe();
1082 char *printername
= NULL
;
1083 http_t
*http
= NULL
; /* HTTP connection to server */
1084 ipp_t
*request
= NULL
, /* IPP Request */
1085 *response
= NULL
; /* IPP Response */
1086 ipp_attribute_t
*attr
= NULL
; /* Current attribute */
1087 cups_lang_t
*language
= NULL
; /* Default language */
1088 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
1089 int qcount
= 0, /* Number of active queue entries */
1090 qalloc
= 0; /* Number of queue entries allocated */
1091 print_queue_struct
*queue
= NULL
, /* Queue entries */
1092 *temp
; /* Temporary pointer for queue */
1093 char *user_name
= NULL
, /* job-originating-user-name attribute */
1094 *job_name
= NULL
; /* job-name attribute */
1095 int job_id
; /* job-id attribute */
1096 int job_k_octets
; /* job-k-octets attribute */
1097 time_t job_time
; /* time-at-creation attribute */
1098 ipp_jstate_t job_status
; /* job-status attribute */
1099 int job_priority
; /* job-priority attribute */
1101 static const char *jattrs
[] = /* Requested job attributes */
1106 "job-originating-user-name",
1111 static const char *pattrs
[] = /* Requested printer attributes */
1114 "printer-state-message"
1119 /* HACK ALERT!!! The problem with support the 'printer name'
1120 option is that we key the tdb off the sharename. So we will
1121 overload the lpq_command string to pass in the printername
1122 (which is basically what we do for non-cups printers ... using
1123 the lpq_command to get the queue listing). */
1125 if (!push_utf8_talloc(frame
, &printername
, lpq_command
, &size
)) {
1128 DEBUG(5,("cups_queue_get(%s, %p, %p)\n", lpq_command
, q
, status
));
1131 * Make sure we don't ask for passwords...
1134 cupsSetPasswordCB(cups_passwd_cb
);
1137 * Try to connect to the server...
1140 if ((http
= cups_connect(frame
)) == NULL
) {
1145 * Generate the printer URI...
1148 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/printers/%s", printername
);
1151 * Build an IPP_GET_JOBS request, which requires the following
1154 * attributes-charset
1155 * attributes-natural-language
1156 * requested-attributes
1162 request
->request
.op
.operation_id
= IPP_GET_JOBS
;
1163 request
->request
.op
.request_id
= 1;
1165 language
= cupsLangDefault();
1167 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1168 "attributes-charset", NULL
, "utf-8");
1170 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1171 "attributes-natural-language", NULL
, language
->language
);
1173 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1174 "requested-attributes",
1175 (sizeof(jattrs
) / sizeof(jattrs
[0])),
1178 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1179 "printer-uri", NULL
, uri
);
1182 * Do the request and get back a response...
1185 if ((response
= cupsDoRequest(http
, request
, "/")) == NULL
) {
1186 DEBUG(0,("Unable to get jobs for %s - %s\n", uri
,
1187 ippErrorString(cupsLastError())));
1191 if (response
->request
.status
.status_code
>= IPP_OK_CONFLICT
) {
1192 DEBUG(0,("Unable to get jobs for %s - %s\n", uri
,
1193 ippErrorString(response
->request
.status
.status_code
)));
1198 * Process the jobs...
1205 for (attr
= response
->attrs
; attr
!= NULL
; attr
= attr
->next
) {
1207 * Skip leading attributes until we hit a job...
1210 while (attr
!= NULL
&& attr
->group_tag
!= IPP_TAG_JOB
)
1217 * Allocate memory as needed...
1219 if (qcount
>= qalloc
) {
1222 queue
= SMB_REALLOC_ARRAY(queue
, print_queue_struct
, qalloc
);
1224 if (queue
== NULL
) {
1225 DEBUG(0,("cups_queue_get: Not enough memory!"));
1231 temp
= queue
+ qcount
;
1232 memset(temp
, 0, sizeof(print_queue_struct
));
1235 * Pull the needed attributes from this job...
1240 job_status
= IPP_JOB_PENDING
;
1246 while (attr
!= NULL
&& attr
->group_tag
== IPP_TAG_JOB
) {
1247 if (attr
->name
== NULL
) {
1252 if (strcmp(attr
->name
, "job-id") == 0 &&
1253 attr
->value_tag
== IPP_TAG_INTEGER
)
1254 job_id
= attr
->values
[0].integer
;
1256 if (strcmp(attr
->name
, "job-k-octets") == 0 &&
1257 attr
->value_tag
== IPP_TAG_INTEGER
)
1258 job_k_octets
= attr
->values
[0].integer
;
1260 if (strcmp(attr
->name
, "job-priority") == 0 &&
1261 attr
->value_tag
== IPP_TAG_INTEGER
)
1262 job_priority
= attr
->values
[0].integer
;
1264 if (strcmp(attr
->name
, "job-state") == 0 &&
1265 attr
->value_tag
== IPP_TAG_ENUM
)
1266 job_status
= (ipp_jstate_t
)(attr
->values
[0].integer
);
1268 if (strcmp(attr
->name
, "time-at-creation") == 0 &&
1269 attr
->value_tag
== IPP_TAG_INTEGER
)
1270 job_time
= attr
->values
[0].integer
;
1272 if (strcmp(attr
->name
, "job-name") == 0 &&
1273 attr
->value_tag
== IPP_TAG_NAME
) {
1274 if (!pull_utf8_talloc(frame
,
1276 attr
->values
[0].string
.text
,
1282 if (strcmp(attr
->name
, "job-originating-user-name") == 0 &&
1283 attr
->value_tag
== IPP_TAG_NAME
) {
1284 if (!pull_utf8_talloc(frame
,
1286 attr
->values
[0].string
.text
,
1296 * See if we have everything needed...
1299 if (user_name
== NULL
|| job_name
== NULL
|| job_id
== 0) {
1307 temp
->size
= job_k_octets
* 1024;
1308 temp
->status
= job_status
== IPP_JOB_PENDING
? LPQ_QUEUED
:
1309 job_status
== IPP_JOB_STOPPED
? LPQ_PAUSED
:
1310 job_status
== IPP_JOB_HELD
? LPQ_PAUSED
:
1312 temp
->priority
= job_priority
;
1313 temp
->time
= job_time
;
1314 strlcpy(temp
->fs_user
, user_name
, sizeof(temp
->fs_user
));
1315 strlcpy(temp
->fs_file
, job_name
, sizeof(temp
->fs_file
));
1323 ippDelete(response
);
1327 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the
1328 * following attributes:
1330 * attributes-charset
1331 * attributes-natural-language
1332 * requested-attributes
1338 request
->request
.op
.operation_id
= IPP_GET_PRINTER_ATTRIBUTES
;
1339 request
->request
.op
.request_id
= 1;
1341 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1342 "attributes-charset", NULL
, "utf-8");
1344 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1345 "attributes-natural-language", NULL
, language
->language
);
1347 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1348 "requested-attributes",
1349 (sizeof(pattrs
) / sizeof(pattrs
[0])),
1352 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1353 "printer-uri", NULL
, uri
);
1356 * Do the request and get back a response...
1359 if ((response
= cupsDoRequest(http
, request
, "/")) == NULL
) {
1360 DEBUG(0,("Unable to get printer status for %s - %s\n", printername
,
1361 ippErrorString(cupsLastError())));
1365 if (response
->request
.status
.status_code
>= IPP_OK_CONFLICT
) {
1366 DEBUG(0,("Unable to get printer status for %s - %s\n", printername
,
1367 ippErrorString(response
->request
.status
.status_code
)));
1372 * Get the current printer status and convert it to the SAMBA values.
1375 if ((attr
= ippFindAttribute(response
, "printer-state", IPP_TAG_ENUM
)) != NULL
) {
1376 if (attr
->values
[0].integer
== IPP_PRINTER_STOPPED
)
1377 status
->status
= LPSTAT_STOPPED
;
1379 status
->status
= LPSTAT_OK
;
1382 if ((attr
= ippFindAttribute(response
, "printer-state-message",
1383 IPP_TAG_TEXT
)) != NULL
) {
1385 if (!pull_utf8_talloc(frame
, &msg
,
1386 attr
->values
[0].string
.text
,
1392 fstrcpy(status
->message
, msg
);
1398 * Return the job queue...
1404 ippDelete(response
);
1407 cupsLangFree(language
);
1418 * 'cups_queue_pause()' - Pause a print queue.
1421 static int cups_queue_pause(int snum
)
1423 TALLOC_CTX
*frame
= talloc_stackframe();
1424 int ret
= 1; /* Return value */
1425 http_t
*http
= NULL
; /* HTTP connection to server */
1426 ipp_t
*request
= NULL
, /* IPP Request */
1427 *response
= NULL
; /* IPP Response */
1428 cups_lang_t
*language
= NULL
; /* Default language */
1429 char *printername
= NULL
;
1430 char *username
= NULL
;
1431 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
1434 DEBUG(5,("cups_queue_pause(%d)\n", snum
));
1437 * Make sure we don't ask for passwords...
1440 cupsSetPasswordCB(cups_passwd_cb
);
1443 * Try to connect to the server...
1446 if ((http
= cups_connect(frame
)) == NULL
) {
1451 * Build an IPP_PAUSE_PRINTER request, which requires the following
1454 * attributes-charset
1455 * attributes-natural-language
1457 * requesting-user-name
1462 request
->request
.op
.operation_id
= IPP_PAUSE_PRINTER
;
1463 request
->request
.op
.request_id
= 1;
1465 language
= cupsLangDefault();
1467 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1468 "attributes-charset", NULL
, "utf-8");
1470 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1471 "attributes-natural-language", NULL
, language
->language
);
1473 if (!push_utf8_talloc(frame
, &printername
, PRINTERNAME(snum
), &size
)) {
1476 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/printers/%s",
1479 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
1481 if (!push_utf8_talloc(frame
, &username
, current_user_info
.unix_name
, &size
)) {
1484 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
1488 * Do the request and get back a response...
1491 if ((response
= cupsDoRequest(http
, request
, "/admin/")) != NULL
) {
1492 if (response
->request
.status
.status_code
>= IPP_OK_CONFLICT
) {
1493 DEBUG(0,("Unable to pause printer %s - %s\n", PRINTERNAME(snum
),
1494 ippErrorString(cupsLastError())));
1499 DEBUG(0,("Unable to pause printer %s - %s\n", PRINTERNAME(snum
),
1500 ippErrorString(cupsLastError())));
1505 ippDelete(response
);
1508 cupsLangFree(language
);
1519 * 'cups_queue_resume()' - Restart a print queue.
1522 static int cups_queue_resume(int snum
)
1524 TALLOC_CTX
*frame
= talloc_stackframe();
1525 int ret
= 1; /* Return value */
1526 http_t
*http
= NULL
; /* HTTP connection to server */
1527 ipp_t
*request
= NULL
, /* IPP Request */
1528 *response
= NULL
; /* IPP Response */
1529 cups_lang_t
*language
= NULL
; /* Default language */
1530 char *printername
= NULL
;
1531 char *username
= NULL
;
1532 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
1535 DEBUG(5,("cups_queue_resume(%d)\n", snum
));
1538 * Make sure we don't ask for passwords...
1541 cupsSetPasswordCB(cups_passwd_cb
);
1544 * Try to connect to the server...
1547 if ((http
= cups_connect(frame
)) == NULL
) {
1552 * Build an IPP_RESUME_PRINTER request, which requires the following
1555 * attributes-charset
1556 * attributes-natural-language
1558 * requesting-user-name
1563 request
->request
.op
.operation_id
= IPP_RESUME_PRINTER
;
1564 request
->request
.op
.request_id
= 1;
1566 language
= cupsLangDefault();
1568 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1569 "attributes-charset", NULL
, "utf-8");
1571 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1572 "attributes-natural-language", NULL
, language
->language
);
1574 if (!push_utf8_talloc(frame
, &printername
, PRINTERNAME(snum
), &size
)) {
1577 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/printers/%s",
1580 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
1582 if (!push_utf8_talloc(frame
, &username
, current_user_info
.unix_name
, &size
)) {
1585 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
1589 * Do the request and get back a response...
1592 if ((response
= cupsDoRequest(http
, request
, "/admin/")) != NULL
) {
1593 if (response
->request
.status
.status_code
>= IPP_OK_CONFLICT
) {
1594 DEBUG(0,("Unable to resume printer %s - %s\n", PRINTERNAME(snum
),
1595 ippErrorString(cupsLastError())));
1600 DEBUG(0,("Unable to resume printer %s - %s\n", PRINTERNAME(snum
),
1601 ippErrorString(cupsLastError())));
1606 ippDelete(response
);
1609 cupsLangFree(language
);
1618 /*******************************************************************
1619 * CUPS printing interface definitions...
1620 ******************************************************************/
1622 struct printif cups_printif
=
1634 bool cups_pull_comment_location(NT_PRINTER_INFO_LEVEL_2
*printer
)
1636 TALLOC_CTX
*frame
= talloc_stackframe();
1637 http_t
*http
= NULL
; /* HTTP connection to server */
1638 ipp_t
*request
= NULL
, /* IPP Request */
1639 *response
= NULL
; /* IPP Response */
1640 ipp_attribute_t
*attr
; /* Current attribute */
1641 cups_lang_t
*language
= NULL
; /* Default language */
1642 char uri
[HTTP_MAX_URI
];
1643 char *server
= NULL
;
1644 char *sharename
= NULL
;
1646 static const char *requested
[] =/* Requested attributes */
1655 DEBUG(5, ("pulling %s location\n", printer
->sharename
));
1658 * Make sure we don't ask for passwords...
1661 cupsSetPasswordCB(cups_passwd_cb
);
1664 * Try to connect to the server...
1667 if ((http
= cups_connect(frame
)) == NULL
) {
1673 request
->request
.op
.operation_id
= IPP_GET_PRINTER_ATTRIBUTES
;
1674 request
->request
.op
.request_id
= 1;
1676 language
= cupsLangDefault();
1678 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1679 "attributes-charset", NULL
, "utf-8");
1681 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1682 "attributes-natural-language", NULL
, language
->language
);
1684 if (lp_cups_server() != NULL
&& strlen(lp_cups_server()) > 0) {
1685 if (!push_utf8_talloc(frame
, &server
, lp_cups_server(), &size
)) {
1689 server
= talloc_strdup(frame
,cupsServer());
1694 if (!push_utf8_talloc(frame
, &sharename
, printer
->sharename
, &size
)) {
1697 slprintf(uri
, sizeof(uri
) - 1, "ipp://%s/printers/%s",
1700 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1701 "printer-uri", NULL
, uri
);
1703 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1704 "requested-attributes",
1705 (sizeof(requested
) / sizeof(requested
[0])),
1709 * Do the request and get back a response...
1712 if ((response
= cupsDoRequest(http
, request
, "/")) == NULL
) {
1713 DEBUG(0,("Unable to get printer attributes - %s\n",
1714 ippErrorString(cupsLastError())));
1718 for (attr
= response
->attrs
; attr
!= NULL
;) {
1720 * Skip leading attributes until we hit a printer...
1723 while (attr
!= NULL
&& attr
->group_tag
!= IPP_TAG_PRINTER
)
1730 * Pull the needed attributes from this printer...
1733 while ( attr
&& (attr
->group_tag
== IPP_TAG_PRINTER
) ) {
1734 if (strcmp(attr
->name
, "printer-name") == 0 &&
1735 attr
->value_tag
== IPP_TAG_NAME
) {
1736 if (!pull_utf8_talloc(frame
,
1738 attr
->values
[0].string
.text
,
1744 /* Grab the comment if we don't have one */
1745 if ( (strcmp(attr
->name
, "printer-info") == 0)
1746 && (attr
->value_tag
== IPP_TAG_TEXT
)
1747 && !strlen(printer
->comment
) )
1749 char *comment
= NULL
;
1750 if (!pull_utf8_talloc(frame
,
1752 attr
->values
[0].string
.text
,
1756 DEBUG(5,("cups_pull_comment_location: Using cups comment: %s\n",
1758 strlcpy(printer
->comment
,
1760 sizeof(printer
->comment
));
1763 /* Grab the location if we don't have one */
1764 if ( (strcmp(attr
->name
, "printer-location") == 0)
1765 && (attr
->value_tag
== IPP_TAG_TEXT
)
1766 && !strlen(printer
->location
) )
1768 char *location
= NULL
;
1769 if (!pull_utf8_talloc(frame
,
1771 attr
->values
[0].string
.text
,
1775 DEBUG(5,("cups_pull_comment_location: Using cups location: %s\n",
1777 strlcpy(printer
->location
,
1779 sizeof(printer
->location
));
1786 * We have everything needed...
1797 ippDelete(response
);
1804 cupsLangFree(language
);
1814 /* this keeps fussy compilers happy */
1815 void print_cups_dummy(void);
1816 void print_cups_dummy(void) {}
1817 #endif /* HAVE_CUPS */