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 extern userdom_struct current_user_info
;
35 * 'cups_passwd_cb()' - The CUPS password callback...
38 static const char * /* O - Password or NULL */
39 cups_passwd_cb(const char *prompt
) /* I - Prompt */
42 * Always return NULL to indicate that no password is available...
48 static http_t
*cups_connect(TALLOC_CTX
*frame
)
51 char *server
= NULL
, *p
= NULL
;
54 if (lp_cups_server() != NULL
&& strlen(lp_cups_server()) > 0) {
55 if (push_utf8_talloc(frame
, &server
, lp_cups_server()) == (size_t)-1) {
59 server
= talloc_strdup(frame
,cupsServer());
65 p
= strchr(server
, ':');
73 DEBUG(10, ("connecting to cups server %s:%d\n",
76 if ((http
= httpConnect(server
, port
)) == NULL
) {
77 DEBUG(0,("Unable to connect to CUPS server %s:%d - %s\n",
78 server
, port
, strerror(errno
)));
85 static void send_pcap_info(const char *name
, const char *info
, void *pd
)
88 size_t namelen
= name
? strlen(name
)+1 : 0;
89 size_t infolen
= info
? strlen(info
)+1 : 0;
91 DEBUG(11,("send_pcap_info: writing namelen %u\n", (unsigned int)namelen
));
92 if (sys_write(fd
, &namelen
, sizeof(namelen
)) != sizeof(namelen
)) {
93 DEBUG(10,("send_pcap_info: namelen write failed %s\n",
97 DEBUG(11,("send_pcap_info: writing infolen %u\n", (unsigned int)infolen
));
98 if (sys_write(fd
, &infolen
, sizeof(infolen
)) != sizeof(infolen
)) {
99 DEBUG(10,("send_pcap_info: infolen write failed %s\n",
104 DEBUG(11,("send_pcap_info: writing name %s\n", name
));
105 if (sys_write(fd
, name
, namelen
) != namelen
) {
106 DEBUG(10,("send_pcap_info: name write failed %s\n",
112 DEBUG(11,("send_pcap_info: writing info %s\n", info
));
113 if (sys_write(fd
, info
, infolen
) != infolen
) {
114 DEBUG(10,("send_pcap_info: info write failed %s\n",
121 static bool cups_cache_reload_async(int fd
)
123 TALLOC_CTX
*frame
= talloc_stackframe();
124 struct pcap_cache
*tmp_pcap_cache
= NULL
;
125 http_t
*http
= NULL
; /* HTTP connection to server */
126 ipp_t
*request
= NULL
, /* IPP Request */
127 *response
= NULL
; /* IPP Response */
128 ipp_attribute_t
*attr
; /* Current attribute */
129 cups_lang_t
*language
= NULL
; /* Default language */
130 char *name
, /* printer-name attribute */
131 *info
; /* printer-info attribute */
132 static const char *requested
[] =/* Requested attributes */
139 DEBUG(5, ("reloading cups printcap cache\n"));
142 * Make sure we don't ask for passwords...
145 cupsSetPasswordCB(cups_passwd_cb
);
148 * Try to connect to the server...
151 if ((http
= cups_connect(frame
)) == NULL
) {
156 * Build a CUPS_GET_PRINTERS request, which requires the following
160 * attributes-natural-language
161 * requested-attributes
166 request
->request
.op
.operation_id
= CUPS_GET_PRINTERS
;
167 request
->request
.op
.request_id
= 1;
169 language
= cupsLangDefault();
171 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
172 "attributes-charset", NULL
, "utf-8");
174 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
175 "attributes-natural-language", NULL
, language
->language
);
177 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
178 "requested-attributes",
179 (sizeof(requested
) / sizeof(requested
[0])),
183 * Do the request and get back a response...
186 if ((response
= cupsDoRequest(http
, request
, "/")) == NULL
) {
187 DEBUG(0,("Unable to get printer list - %s\n",
188 ippErrorString(cupsLastError())));
192 for (attr
= response
->attrs
; attr
!= NULL
;) {
194 * Skip leading attributes until we hit a printer...
197 while (attr
!= NULL
&& attr
->group_tag
!= IPP_TAG_PRINTER
)
204 * Pull the needed attributes from this printer...
210 while (attr
!= NULL
&& attr
->group_tag
== IPP_TAG_PRINTER
) {
211 if (strcmp(attr
->name
, "printer-name") == 0 &&
212 attr
->value_tag
== IPP_TAG_NAME
) {
213 pull_utf8_talloc(frame
,
215 attr
->values
[0].string
.text
);
218 if (strcmp(attr
->name
, "printer-info") == 0 &&
219 attr
->value_tag
== IPP_TAG_TEXT
) {
220 pull_utf8_talloc(frame
,
222 attr
->values
[0].string
.text
);
229 * See if we have everything needed...
235 if (!pcap_cache_add_specific(&tmp_pcap_cache
, name
, info
)) {
244 * Build a CUPS_GET_CLASSES request, which requires the following
248 * attributes-natural-language
249 * requested-attributes
254 request
->request
.op
.operation_id
= CUPS_GET_CLASSES
;
255 request
->request
.op
.request_id
= 1;
257 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
258 "attributes-charset", NULL
, "utf-8");
260 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
261 "attributes-natural-language", NULL
, language
->language
);
263 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
264 "requested-attributes",
265 (sizeof(requested
) / sizeof(requested
[0])),
269 * Do the request and get back a response...
272 if ((response
= cupsDoRequest(http
, request
, "/")) == NULL
) {
273 DEBUG(0,("Unable to get printer list - %s\n",
274 ippErrorString(cupsLastError())));
278 for (attr
= response
->attrs
; attr
!= NULL
;) {
280 * Skip leading attributes until we hit a printer...
283 while (attr
!= NULL
&& attr
->group_tag
!= IPP_TAG_PRINTER
)
290 * Pull the needed attributes from this printer...
296 while (attr
!= NULL
&& attr
->group_tag
== IPP_TAG_PRINTER
) {
297 if (strcmp(attr
->name
, "printer-name") == 0 &&
298 attr
->value_tag
== IPP_TAG_NAME
) {
299 pull_utf8_talloc(frame
,
301 attr
->values
[0].string
.text
);
304 if (strcmp(attr
->name
, "printer-info") == 0 &&
305 attr
->value_tag
== IPP_TAG_TEXT
) {
306 pull_utf8_talloc(frame
,
308 attr
->values
[0].string
.text
);
315 * See if we have everything needed...
321 if (!pcap_cache_add_specific(&tmp_pcap_cache
, name
, info
)) {
333 cupsLangFree(language
);
338 /* Send all the entries up the pipe. */
339 if (tmp_pcap_cache
) {
340 pcap_printer_fn_specific(tmp_pcap_cache
,
344 pcap_cache_destroy_specific(&tmp_pcap_cache
);
350 static struct pcap_cache
*local_pcap_copy
;
351 struct fd_event
*cache_fd_event
;
353 static bool cups_pcap_load_async(int *pfd
)
360 if (cache_fd_event
) {
361 DEBUG(3,("cups_pcap_load_async: already waiting for "
362 "a refresh event\n" ));
366 DEBUG(5,("cups_pcap_load_async: asynchronously loading cups printers\n"));
368 if (pipe(fds
) == -1) {
373 if (pid
== (pid_t
)-1) {
374 DEBUG(10,("cups_pcap_load_async: fork failed %s\n",
382 DEBUG(10,("cups_pcap_load_async: child pid = %u\n",
383 (unsigned int)pid
));
391 if (!reinit_after_fork(smbd_messaging_context(), true)) {
392 DEBUG(0,("cups_pcap_load_async: reinit_after_fork() failed\n"));
393 smb_panic("cups_pcap_load_async: reinit_after_fork() failed");
397 cups_cache_reload_async(fds
[1]);
402 static void cups_async_callback(struct event_context
*event_ctx
,
403 struct fd_event
*event
,
407 TALLOC_CTX
*frame
= talloc_stackframe();
409 struct pcap_cache
*tmp_pcap_cache
= NULL
;
411 DEBUG(5,("cups_async_callback: callback received for printer data. "
415 char *name
= NULL
, *info
= NULL
;
416 size_t namelen
= 0, infolen
= 0;
419 ret
= sys_read(fd
, &namelen
, sizeof(namelen
));
424 if (ret
!= sizeof(namelen
)) {
425 DEBUG(10,("cups_async_callback: namelen read failed %d %s\n",
426 errno
, strerror(errno
)));
430 DEBUG(11,("cups_async_callback: read namelen %u\n",
431 (unsigned int)namelen
));
433 ret
= sys_read(fd
, &infolen
, sizeof(infolen
));
438 if (ret
!= sizeof(infolen
)) {
439 DEBUG(10,("cups_async_callback: infolen read failed %s\n",
444 DEBUG(11,("cups_async_callback: read infolen %u\n",
445 (unsigned int)infolen
));
448 name
= TALLOC_ARRAY(frame
, char, namelen
);
452 ret
= sys_read(fd
, name
, namelen
);
457 if (ret
!= namelen
) {
458 DEBUG(10,("cups_async_callback: name read failed %s\n",
462 DEBUG(11,("cups_async_callback: read name %s\n",
468 info
= TALLOC_ARRAY(frame
, char, infolen
);
472 ret
= sys_read(fd
, info
, infolen
);
477 if (ret
!= infolen
) {
478 DEBUG(10,("cups_async_callback: info read failed %s\n",
482 DEBUG(11,("cups_async_callback: read info %s\n",
488 /* Add to our local pcap cache. */
489 pcap_cache_add_specific(&tmp_pcap_cache
, name
, info
);
495 if (tmp_pcap_cache
) {
496 /* We got a namelist, replace our local cache. */
497 pcap_cache_destroy_specific(&local_pcap_copy
);
498 local_pcap_copy
= tmp_pcap_cache
;
500 /* And the systemwide pcap cache. */
501 pcap_cache_replace(local_pcap_copy
);
503 DEBUG(2,("cups_async_callback: failed to read a new "
508 TALLOC_FREE(cache_fd_event
);
511 bool cups_cache_reload(void)
513 int *p_pipe_fd
= TALLOC_P(NULL
, int);
521 /* Set up an async refresh. */
522 if (!cups_pcap_load_async(p_pipe_fd
)) {
525 if (!local_pcap_copy
) {
526 /* We have no local cache, wait directly for
527 * async refresh to complete.
529 DEBUG(10,("cups_cache_reload: sync read on fd %d\n",
532 cups_async_callback(smbd_event_context(),
536 if (!local_pcap_copy
) {
540 /* Replace the system cache with our
542 pcap_cache_replace(local_pcap_copy
);
544 DEBUG(10,("cups_cache_reload: async read on fd %d\n",
547 /* Trigger an event when the pipe can be read. */
548 cache_fd_event
= event_add_fd(smbd_event_context(),
553 if (!cache_fd_event
) {
555 TALLOC_FREE(p_pipe_fd
);
563 * 'cups_job_delete()' - Delete a job.
566 static int cups_job_delete(const char *sharename
, const char *lprm_command
, struct printjob
*pjob
)
568 TALLOC_CTX
*frame
= talloc_stackframe();
569 int ret
= 1; /* Return value */
570 http_t
*http
= NULL
; /* HTTP connection to server */
571 ipp_t
*request
= NULL
, /* IPP Request */
572 *response
= NULL
; /* IPP Response */
573 cups_lang_t
*language
= NULL
; /* Default language */
575 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
578 DEBUG(5,("cups_job_delete(%s, %p (%d))\n", sharename
, pjob
, pjob
->sysjob
));
581 * Make sure we don't ask for passwords...
584 cupsSetPasswordCB(cups_passwd_cb
);
587 * Try to connect to the server...
590 if ((http
= cups_connect(frame
)) == NULL
) {
595 * Build an IPP_CANCEL_JOB request, which requires the following
599 * attributes-natural-language
601 * requesting-user-name
606 request
->request
.op
.operation_id
= IPP_CANCEL_JOB
;
607 request
->request
.op
.request_id
= 1;
609 language
= cupsLangDefault();
611 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
612 "attributes-charset", NULL
, "utf-8");
614 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
615 "attributes-natural-language", NULL
, language
->language
);
617 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/jobs/%d", pjob
->sysjob
);
619 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
621 if (push_utf8_talloc(frame
, &user
, pjob
->user
) == (size_t)-1) {
625 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
629 * Do the request and get back a response...
632 if ((response
= cupsDoRequest(http
, request
, "/jobs")) != NULL
) {
633 if (response
->request
.status
.status_code
>= IPP_OK_CONFLICT
) {
634 DEBUG(0,("Unable to cancel job %d - %s\n", pjob
->sysjob
,
635 ippErrorString(cupsLastError())));
640 DEBUG(0,("Unable to cancel job %d - %s\n", pjob
->sysjob
,
641 ippErrorString(cupsLastError())));
649 cupsLangFree(language
);
660 * 'cups_job_pause()' - Pause a job.
663 static int cups_job_pause(int snum
, struct printjob
*pjob
)
665 TALLOC_CTX
*frame
= talloc_stackframe();
666 int ret
= 1; /* Return value */
667 http_t
*http
= NULL
; /* HTTP connection to server */
668 ipp_t
*request
= NULL
, /* IPP Request */
669 *response
= NULL
; /* IPP Response */
670 cups_lang_t
*language
= NULL
; /* Default language */
672 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
675 DEBUG(5,("cups_job_pause(%d, %p (%d))\n", snum
, pjob
, pjob
->sysjob
));
678 * Make sure we don't ask for passwords...
681 cupsSetPasswordCB(cups_passwd_cb
);
684 * Try to connect to the server...
687 if ((http
= cups_connect(frame
)) == NULL
) {
692 * Build an IPP_HOLD_JOB request, which requires the following
696 * attributes-natural-language
698 * requesting-user-name
703 request
->request
.op
.operation_id
= IPP_HOLD_JOB
;
704 request
->request
.op
.request_id
= 1;
706 language
= cupsLangDefault();
708 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
709 "attributes-charset", NULL
, "utf-8");
711 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
712 "attributes-natural-language", NULL
, language
->language
);
714 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/jobs/%d", pjob
->sysjob
);
716 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
718 if (push_utf8_talloc(frame
, &user
, pjob
->user
) == (size_t)-1) {
721 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
725 * Do the request and get back a response...
728 if ((response
= cupsDoRequest(http
, request
, "/jobs")) != NULL
) {
729 if (response
->request
.status
.status_code
>= IPP_OK_CONFLICT
) {
730 DEBUG(0,("Unable to hold job %d - %s\n", pjob
->sysjob
,
731 ippErrorString(cupsLastError())));
736 DEBUG(0,("Unable to hold job %d - %s\n", pjob
->sysjob
,
737 ippErrorString(cupsLastError())));
745 cupsLangFree(language
);
756 * 'cups_job_resume()' - Resume a paused job.
759 static int cups_job_resume(int snum
, struct printjob
*pjob
)
761 TALLOC_CTX
*frame
= talloc_stackframe();
762 int ret
= 1; /* Return value */
763 http_t
*http
= NULL
; /* HTTP connection to server */
764 ipp_t
*request
= NULL
, /* IPP Request */
765 *response
= NULL
; /* IPP Response */
766 cups_lang_t
*language
= NULL
; /* Default language */
768 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
771 DEBUG(5,("cups_job_resume(%d, %p (%d))\n", snum
, pjob
, pjob
->sysjob
));
774 * Make sure we don't ask for passwords...
777 cupsSetPasswordCB(cups_passwd_cb
);
780 * Try to connect to the server...
783 if ((http
= cups_connect(frame
)) == NULL
) {
788 * Build an IPP_RELEASE_JOB request, which requires the following
792 * attributes-natural-language
794 * requesting-user-name
799 request
->request
.op
.operation_id
= IPP_RELEASE_JOB
;
800 request
->request
.op
.request_id
= 1;
802 language
= cupsLangDefault();
804 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
805 "attributes-charset", NULL
, "utf-8");
807 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
808 "attributes-natural-language", NULL
, language
->language
);
810 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/jobs/%d", pjob
->sysjob
);
812 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
814 if (push_utf8_talloc(frame
, &user
, pjob
->user
) == (size_t)-1) {
817 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
821 * Do the request and get back a response...
824 if ((response
= cupsDoRequest(http
, request
, "/jobs")) != NULL
) {
825 if (response
->request
.status
.status_code
>= IPP_OK_CONFLICT
) {
826 DEBUG(0,("Unable to release job %d - %s\n", pjob
->sysjob
,
827 ippErrorString(cupsLastError())));
832 DEBUG(0,("Unable to release job %d - %s\n", pjob
->sysjob
,
833 ippErrorString(cupsLastError())));
841 cupsLangFree(language
);
852 * 'cups_job_submit()' - Submit a job for printing.
855 static int cups_job_submit(int snum
, struct printjob
*pjob
)
857 TALLOC_CTX
*frame
= talloc_stackframe();
858 int ret
= 1; /* Return value */
859 http_t
*http
= NULL
; /* HTTP connection to server */
860 ipp_t
*request
= NULL
, /* IPP Request */
861 *response
= NULL
; /* IPP Response */
862 cups_lang_t
*language
= NULL
; /* Default language */
863 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
864 const char *clientname
= NULL
; /* hostname of client for job-originating-host attribute */
865 char *new_jobname
= NULL
;
867 cups_option_t
*options
= NULL
;
868 char *printername
= NULL
;
870 char *jobname
= NULL
;
871 char *cupsoptions
= NULL
;
872 char *filename
= NULL
;
873 char addr
[INET6_ADDRSTRLEN
];
875 DEBUG(5,("cups_job_submit(%d, %p (%d))\n", snum
, pjob
, pjob
->sysjob
));
878 * Make sure we don't ask for passwords...
881 cupsSetPasswordCB(cups_passwd_cb
);
884 * Try to connect to the server...
887 if ((http
= cups_connect(frame
)) == NULL
) {
892 * Build an IPP_PRINT_JOB request, which requires the following
896 * attributes-natural-language
898 * requesting-user-name
904 request
->request
.op
.operation_id
= IPP_PRINT_JOB
;
905 request
->request
.op
.request_id
= 1;
907 language
= cupsLangDefault();
909 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
910 "attributes-charset", NULL
, "utf-8");
912 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
913 "attributes-natural-language", NULL
, language
->language
);
915 if (push_utf8_talloc(frame
, &printername
, PRINTERNAME(snum
)) == (size_t)-1) {
918 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/printers/%s",
921 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
922 "printer-uri", NULL
, uri
);
924 if (push_utf8_talloc(frame
, &user
, pjob
->user
) == (size_t)-1) {
927 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
930 clientname
= client_name(get_client_fd());
931 if (strcmp(clientname
, "UNKNOWN") == 0) {
932 clientname
= client_addr(get_client_fd(),addr
,sizeof(addr
));
935 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
936 "job-originating-host-name", NULL
,
939 if (push_utf8_talloc(frame
, &jobname
, pjob
->jobname
) == (size_t)-1) {
942 new_jobname
= talloc_asprintf(frame
,
943 "%s%.8u %s", PRINT_SPOOL_PREFIX
,
944 (unsigned int)pjob
->smbjob
,
946 if (new_jobname
== NULL
) {
950 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name", NULL
,
954 * add any options defined in smb.conf
957 if (push_utf8_talloc(frame
, &cupsoptions
, lp_cups_options(snum
)) == (size_t)-1) {
962 num_options
= cupsParseOptions(cupsoptions
, num_options
, &options
);
965 cupsEncodeOptions(request
, num_options
, options
);
968 * Do the request and get back a response...
971 slprintf(uri
, sizeof(uri
) - 1, "/printers/%s", printername
);
973 if (push_utf8_talloc(frame
, &filename
, pjob
->filename
) == (size_t)-1) {
976 if ((response
= cupsDoFileRequest(http
, request
, uri
, pjob
->filename
)) != NULL
) {
977 if (response
->request
.status
.status_code
>= IPP_OK_CONFLICT
) {
978 DEBUG(0,("Unable to print file to %s - %s\n", PRINTERNAME(snum
),
979 ippErrorString(cupsLastError())));
984 DEBUG(0,("Unable to print file to `%s' - %s\n", PRINTERNAME(snum
),
985 ippErrorString(cupsLastError())));
989 unlink(pjob
->filename
);
990 /* else print_job_end will do it for us */
997 cupsLangFree(language
);
1008 * 'cups_queue_get()' - Get all the jobs in the print queue.
1011 static int cups_queue_get(const char *sharename
,
1012 enum printing_types printing_type
,
1014 print_queue_struct
**q
,
1015 print_status_struct
*status
)
1017 TALLOC_CTX
*frame
= talloc_stackframe();
1018 char *printername
= NULL
;
1019 http_t
*http
= NULL
; /* HTTP connection to server */
1020 ipp_t
*request
= NULL
, /* IPP Request */
1021 *response
= NULL
; /* IPP Response */
1022 ipp_attribute_t
*attr
= NULL
; /* Current attribute */
1023 cups_lang_t
*language
= NULL
; /* Default language */
1024 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
1025 int qcount
= 0, /* Number of active queue entries */
1026 qalloc
= 0; /* Number of queue entries allocated */
1027 print_queue_struct
*queue
= NULL
, /* Queue entries */
1028 *temp
; /* Temporary pointer for queue */
1029 char *user_name
= NULL
, /* job-originating-user-name attribute */
1030 *job_name
= NULL
; /* job-name attribute */
1031 int job_id
; /* job-id attribute */
1032 int job_k_octets
; /* job-k-octets attribute */
1033 time_t job_time
; /* time-at-creation attribute */
1034 ipp_jstate_t job_status
; /* job-status attribute */
1035 int job_priority
; /* job-priority attribute */
1036 static const char *jattrs
[] = /* Requested job attributes */
1041 "job-originating-user-name",
1046 static const char *pattrs
[] = /* Requested printer attributes */
1049 "printer-state-message"
1054 /* HACK ALERT!!! The problem with support the 'printer name'
1055 option is that we key the tdb off the sharename. So we will
1056 overload the lpq_command string to pass in the printername
1057 (which is basically what we do for non-cups printers ... using
1058 the lpq_command to get the queue listing). */
1060 if (push_utf8_talloc(frame
, &printername
, lpq_command
) == (size_t)-1) {
1063 DEBUG(5,("cups_queue_get(%s, %p, %p)\n", lpq_command
, q
, status
));
1066 * Make sure we don't ask for passwords...
1069 cupsSetPasswordCB(cups_passwd_cb
);
1072 * Try to connect to the server...
1075 if ((http
= cups_connect(frame
)) == NULL
) {
1080 * Generate the printer URI...
1083 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/printers/%s", printername
);
1086 * Build an IPP_GET_JOBS request, which requires the following
1089 * attributes-charset
1090 * attributes-natural-language
1091 * requested-attributes
1097 request
->request
.op
.operation_id
= IPP_GET_JOBS
;
1098 request
->request
.op
.request_id
= 1;
1100 language
= cupsLangDefault();
1102 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1103 "attributes-charset", NULL
, "utf-8");
1105 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1106 "attributes-natural-language", NULL
, language
->language
);
1108 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1109 "requested-attributes",
1110 (sizeof(jattrs
) / sizeof(jattrs
[0])),
1113 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1114 "printer-uri", NULL
, uri
);
1117 * Do the request and get back a response...
1120 if ((response
= cupsDoRequest(http
, request
, "/")) == NULL
) {
1121 DEBUG(0,("Unable to get jobs for %s - %s\n", uri
,
1122 ippErrorString(cupsLastError())));
1126 if (response
->request
.status
.status_code
>= IPP_OK_CONFLICT
) {
1127 DEBUG(0,("Unable to get jobs for %s - %s\n", uri
,
1128 ippErrorString(response
->request
.status
.status_code
)));
1133 * Process the jobs...
1140 for (attr
= response
->attrs
; attr
!= NULL
; attr
= attr
->next
) {
1142 * Skip leading attributes until we hit a job...
1145 while (attr
!= NULL
&& attr
->group_tag
!= IPP_TAG_JOB
)
1152 * Allocate memory as needed...
1154 if (qcount
>= qalloc
) {
1157 queue
= SMB_REALLOC_ARRAY(queue
, print_queue_struct
, qalloc
);
1159 if (queue
== NULL
) {
1160 DEBUG(0,("cups_queue_get: Not enough memory!"));
1166 temp
= queue
+ qcount
;
1167 memset(temp
, 0, sizeof(print_queue_struct
));
1170 * Pull the needed attributes from this job...
1175 job_status
= IPP_JOB_PENDING
;
1181 while (attr
!= NULL
&& attr
->group_tag
== IPP_TAG_JOB
) {
1182 if (attr
->name
== NULL
) {
1187 if (strcmp(attr
->name
, "job-id") == 0 &&
1188 attr
->value_tag
== IPP_TAG_INTEGER
)
1189 job_id
= attr
->values
[0].integer
;
1191 if (strcmp(attr
->name
, "job-k-octets") == 0 &&
1192 attr
->value_tag
== IPP_TAG_INTEGER
)
1193 job_k_octets
= attr
->values
[0].integer
;
1195 if (strcmp(attr
->name
, "job-priority") == 0 &&
1196 attr
->value_tag
== IPP_TAG_INTEGER
)
1197 job_priority
= attr
->values
[0].integer
;
1199 if (strcmp(attr
->name
, "job-state") == 0 &&
1200 attr
->value_tag
== IPP_TAG_ENUM
)
1201 job_status
= (ipp_jstate_t
)(attr
->values
[0].integer
);
1203 if (strcmp(attr
->name
, "time-at-creation") == 0 &&
1204 attr
->value_tag
== IPP_TAG_INTEGER
)
1205 job_time
= attr
->values
[0].integer
;
1207 if (strcmp(attr
->name
, "job-name") == 0 &&
1208 attr
->value_tag
== IPP_TAG_NAME
) {
1209 pull_utf8_talloc(frame
,
1211 attr
->values
[0].string
.text
);
1214 if (strcmp(attr
->name
, "job-originating-user-name") == 0 &&
1215 attr
->value_tag
== IPP_TAG_NAME
) {
1216 pull_utf8_talloc(frame
,
1218 attr
->values
[0].string
.text
);
1225 * See if we have everything needed...
1228 if (user_name
== NULL
|| job_name
== NULL
|| job_id
== 0) {
1236 temp
->size
= job_k_octets
* 1024;
1237 temp
->status
= job_status
== IPP_JOB_PENDING
? LPQ_QUEUED
:
1238 job_status
== IPP_JOB_STOPPED
? LPQ_PAUSED
:
1239 job_status
== IPP_JOB_HELD
? LPQ_PAUSED
:
1241 temp
->priority
= job_priority
;
1242 temp
->time
= job_time
;
1243 strlcpy(temp
->fs_user
, user_name
, sizeof(temp
->fs_user
));
1244 strlcpy(temp
->fs_file
, job_name
, sizeof(temp
->fs_file
));
1252 ippDelete(response
);
1256 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the
1257 * following attributes:
1259 * attributes-charset
1260 * attributes-natural-language
1261 * requested-attributes
1267 request
->request
.op
.operation_id
= IPP_GET_PRINTER_ATTRIBUTES
;
1268 request
->request
.op
.request_id
= 1;
1270 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1271 "attributes-charset", NULL
, "utf-8");
1273 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1274 "attributes-natural-language", NULL
, language
->language
);
1276 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1277 "requested-attributes",
1278 (sizeof(pattrs
) / sizeof(pattrs
[0])),
1281 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1282 "printer-uri", NULL
, uri
);
1285 * Do the request and get back a response...
1288 if ((response
= cupsDoRequest(http
, request
, "/")) == NULL
) {
1289 DEBUG(0,("Unable to get printer status for %s - %s\n", printername
,
1290 ippErrorString(cupsLastError())));
1295 if (response
->request
.status
.status_code
>= IPP_OK_CONFLICT
) {
1296 DEBUG(0,("Unable to get printer status for %s - %s\n", printername
,
1297 ippErrorString(response
->request
.status
.status_code
)));
1303 * Get the current printer status and convert it to the SAMBA values.
1306 if ((attr
= ippFindAttribute(response
, "printer-state", IPP_TAG_ENUM
)) != NULL
) {
1307 if (attr
->values
[0].integer
== IPP_PRINTER_STOPPED
)
1308 status
->status
= LPSTAT_STOPPED
;
1310 status
->status
= LPSTAT_OK
;
1313 if ((attr
= ippFindAttribute(response
, "printer-state-message",
1314 IPP_TAG_TEXT
)) != NULL
) {
1316 pull_utf8_talloc(frame
, &msg
, attr
->values
[0].string
.text
);
1317 fstrcpy(status
->message
, msg
);
1321 * Return the job queue...
1328 ippDelete(response
);
1331 cupsLangFree(language
);
1342 * 'cups_queue_pause()' - Pause a print queue.
1345 static int cups_queue_pause(int snum
)
1347 TALLOC_CTX
*frame
= talloc_stackframe();
1348 int ret
= 1; /* Return value */
1349 http_t
*http
= NULL
; /* HTTP connection to server */
1350 ipp_t
*request
= NULL
, /* IPP Request */
1351 *response
= NULL
; /* IPP Response */
1352 cups_lang_t
*language
= NULL
; /* Default language */
1353 char *printername
= NULL
;
1354 char *username
= NULL
;
1355 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
1358 DEBUG(5,("cups_queue_pause(%d)\n", snum
));
1361 * Make sure we don't ask for passwords...
1364 cupsSetPasswordCB(cups_passwd_cb
);
1367 * Try to connect to the server...
1370 if ((http
= cups_connect(frame
)) == NULL
) {
1375 * Build an IPP_PAUSE_PRINTER request, which requires the following
1378 * attributes-charset
1379 * attributes-natural-language
1381 * requesting-user-name
1386 request
->request
.op
.operation_id
= IPP_PAUSE_PRINTER
;
1387 request
->request
.op
.request_id
= 1;
1389 language
= cupsLangDefault();
1391 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1392 "attributes-charset", NULL
, "utf-8");
1394 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1395 "attributes-natural-language", NULL
, language
->language
);
1397 if (push_utf8_talloc(frame
, &printername
, PRINTERNAME(snum
)) == (size_t)-1) {
1400 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/printers/%s",
1403 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
1405 if (push_utf8_talloc(frame
, &username
, current_user_info
.unix_name
) == (size_t)-1) {
1408 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
1412 * Do the request and get back a response...
1415 if ((response
= cupsDoRequest(http
, request
, "/admin/")) != NULL
) {
1416 if (response
->request
.status
.status_code
>= IPP_OK_CONFLICT
) {
1417 DEBUG(0,("Unable to pause printer %s - %s\n", PRINTERNAME(snum
),
1418 ippErrorString(cupsLastError())));
1423 DEBUG(0,("Unable to pause printer %s - %s\n", PRINTERNAME(snum
),
1424 ippErrorString(cupsLastError())));
1429 ippDelete(response
);
1432 cupsLangFree(language
);
1443 * 'cups_queue_resume()' - Restart a print queue.
1446 static int cups_queue_resume(int snum
)
1448 TALLOC_CTX
*frame
= talloc_stackframe();
1449 int ret
= 1; /* Return value */
1450 http_t
*http
= NULL
; /* HTTP connection to server */
1451 ipp_t
*request
= NULL
, /* IPP Request */
1452 *response
= NULL
; /* IPP Response */
1453 cups_lang_t
*language
= NULL
; /* Default language */
1454 char *printername
= NULL
;
1455 char *username
= NULL
;
1456 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
1459 DEBUG(5,("cups_queue_resume(%d)\n", snum
));
1462 * Make sure we don't ask for passwords...
1465 cupsSetPasswordCB(cups_passwd_cb
);
1468 * Try to connect to the server...
1471 if ((http
= cups_connect(frame
)) == NULL
) {
1476 * Build an IPP_RESUME_PRINTER request, which requires the following
1479 * attributes-charset
1480 * attributes-natural-language
1482 * requesting-user-name
1487 request
->request
.op
.operation_id
= IPP_RESUME_PRINTER
;
1488 request
->request
.op
.request_id
= 1;
1490 language
= cupsLangDefault();
1492 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1493 "attributes-charset", NULL
, "utf-8");
1495 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1496 "attributes-natural-language", NULL
, language
->language
);
1498 if (push_utf8_talloc(frame
, &printername
, PRINTERNAME(snum
)) == (size_t)-1) {
1501 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/printers/%s",
1504 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
1506 if (push_utf8_talloc(frame
, &username
, current_user_info
.unix_name
) == (size_t)-1) {
1509 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
1513 * Do the request and get back a response...
1516 if ((response
= cupsDoRequest(http
, request
, "/admin/")) != NULL
) {
1517 if (response
->request
.status
.status_code
>= IPP_OK_CONFLICT
) {
1518 DEBUG(0,("Unable to resume printer %s - %s\n", PRINTERNAME(snum
),
1519 ippErrorString(cupsLastError())));
1524 DEBUG(0,("Unable to resume printer %s - %s\n", PRINTERNAME(snum
),
1525 ippErrorString(cupsLastError())));
1530 ippDelete(response
);
1533 cupsLangFree(language
);
1542 /*******************************************************************
1543 * CUPS printing interface definitions...
1544 ******************************************************************/
1546 struct printif cups_printif
=
1558 bool cups_pull_comment_location(NT_PRINTER_INFO_LEVEL_2
*printer
)
1560 TALLOC_CTX
*frame
= talloc_stackframe();
1561 http_t
*http
= NULL
; /* HTTP connection to server */
1562 ipp_t
*request
= NULL
, /* IPP Request */
1563 *response
= NULL
; /* IPP Response */
1564 ipp_attribute_t
*attr
; /* Current attribute */
1565 cups_lang_t
*language
= NULL
; /* Default language */
1566 char uri
[HTTP_MAX_URI
];
1567 char *server
= NULL
;
1568 char *sharename
= NULL
;
1570 static const char *requested
[] =/* Requested attributes */
1578 DEBUG(5, ("pulling %s location\n", printer
->sharename
));
1581 * Make sure we don't ask for passwords...
1584 cupsSetPasswordCB(cups_passwd_cb
);
1587 * Try to connect to the server...
1590 if ((http
= cups_connect(frame
)) == NULL
) {
1596 request
->request
.op
.operation_id
= IPP_GET_PRINTER_ATTRIBUTES
;
1597 request
->request
.op
.request_id
= 1;
1599 language
= cupsLangDefault();
1601 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1602 "attributes-charset", NULL
, "utf-8");
1604 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1605 "attributes-natural-language", NULL
, language
->language
);
1607 if (lp_cups_server() != NULL
&& strlen(lp_cups_server()) > 0) {
1608 if (push_utf8_talloc(frame
, &server
, lp_cups_server()) == (size_t)-1) {
1612 server
= talloc_strdup(frame
,cupsServer());
1617 if (push_utf8_talloc(frame
, &sharename
, printer
->sharename
) == (size_t)-1) {
1620 slprintf(uri
, sizeof(uri
) - 1, "ipp://%s/printers/%s",
1623 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1624 "printer-uri", NULL
, uri
);
1626 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1627 "requested-attributes",
1628 (sizeof(requested
) / sizeof(requested
[0])),
1632 * Do the request and get back a response...
1635 if ((response
= cupsDoRequest(http
, request
, "/")) == NULL
) {
1636 DEBUG(0,("Unable to get printer attributes - %s\n",
1637 ippErrorString(cupsLastError())));
1641 for (attr
= response
->attrs
; attr
!= NULL
;) {
1643 * Skip leading attributes until we hit a printer...
1646 while (attr
!= NULL
&& attr
->group_tag
!= IPP_TAG_PRINTER
)
1653 * Pull the needed attributes from this printer...
1656 while ( attr
&& (attr
->group_tag
== IPP_TAG_PRINTER
) ) {
1657 if (strcmp(attr
->name
, "printer-name") == 0 &&
1658 attr
->value_tag
== IPP_TAG_NAME
) {
1659 pull_utf8_talloc(frame
,
1661 attr
->values
[0].string
.text
);
1664 /* Grab the comment if we don't have one */
1665 if ( (strcmp(attr
->name
, "printer-info") == 0)
1666 && (attr
->value_tag
== IPP_TAG_TEXT
)
1667 && !strlen(printer
->comment
) )
1669 char *comment
= NULL
;
1670 pull_utf8_talloc(frame
,
1672 attr
->values
[0].string
.text
);
1673 DEBUG(5,("cups_pull_comment_location: Using cups comment: %s\n",
1675 strlcpy(printer
->comment
,
1677 sizeof(printer
->comment
));
1680 /* Grab the location if we don't have one */
1681 if ( (strcmp(attr
->name
, "printer-location") == 0)
1682 && (attr
->value_tag
== IPP_TAG_TEXT
)
1683 && !strlen(printer
->location
) )
1685 char *location
= NULL
;
1686 pull_utf8_talloc(frame
,
1688 attr
->values
[0].string
.text
);
1689 DEBUG(5,("cups_pull_comment_location: Using cups location: %s\n",
1691 strlcpy(printer
->location
,
1693 sizeof(printer
->location
));
1700 * We have everything needed...
1711 ippDelete(response
);
1714 cupsLangFree(language
);
1724 /* this keeps fussy compilers happy */
1725 void print_cups_dummy(void);
1726 void print_cups_dummy(void) {}
1727 #endif /* HAVE_CUPS */