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 close_all_print_db();
393 if (!reinit_after_fork(smbd_messaging_context(), true)) {
394 DEBUG(0,("cups_pcap_load_async: reinit_after_fork() failed\n"));
395 smb_panic("cups_pcap_load_async: reinit_after_fork() failed");
399 cups_cache_reload_async(fds
[1]);
404 static void cups_async_callback(struct event_context
*event_ctx
,
405 struct fd_event
*event
,
409 TALLOC_CTX
*frame
= talloc_stackframe();
411 struct pcap_cache
*tmp_pcap_cache
= NULL
;
413 DEBUG(5,("cups_async_callback: callback received for printer data. "
417 char *name
= NULL
, *info
= NULL
;
418 size_t namelen
= 0, infolen
= 0;
421 ret
= sys_read(fd
, &namelen
, sizeof(namelen
));
426 if (ret
!= sizeof(namelen
)) {
427 DEBUG(10,("cups_async_callback: namelen read failed %d %s\n",
428 errno
, strerror(errno
)));
432 DEBUG(11,("cups_async_callback: read namelen %u\n",
433 (unsigned int)namelen
));
435 ret
= sys_read(fd
, &infolen
, sizeof(infolen
));
440 if (ret
!= sizeof(infolen
)) {
441 DEBUG(10,("cups_async_callback: infolen read failed %s\n",
446 DEBUG(11,("cups_async_callback: read infolen %u\n",
447 (unsigned int)infolen
));
450 name
= TALLOC_ARRAY(frame
, char, namelen
);
454 ret
= sys_read(fd
, name
, namelen
);
459 if (ret
!= namelen
) {
460 DEBUG(10,("cups_async_callback: name read failed %s\n",
464 DEBUG(11,("cups_async_callback: read name %s\n",
470 info
= TALLOC_ARRAY(frame
, char, infolen
);
474 ret
= sys_read(fd
, info
, infolen
);
479 if (ret
!= infolen
) {
480 DEBUG(10,("cups_async_callback: info read failed %s\n",
484 DEBUG(11,("cups_async_callback: read info %s\n",
490 /* Add to our local pcap cache. */
491 pcap_cache_add_specific(&tmp_pcap_cache
, name
, info
);
497 if (tmp_pcap_cache
) {
498 /* We got a namelist, replace our local cache. */
499 pcap_cache_destroy_specific(&local_pcap_copy
);
500 local_pcap_copy
= tmp_pcap_cache
;
502 /* And the systemwide pcap cache. */
503 pcap_cache_replace(local_pcap_copy
);
505 DEBUG(2,("cups_async_callback: failed to read a new "
510 TALLOC_FREE(cache_fd_event
);
513 bool cups_cache_reload(void)
515 int *p_pipe_fd
= TALLOC_P(NULL
, int);
523 /* Set up an async refresh. */
524 if (!cups_pcap_load_async(p_pipe_fd
)) {
527 if (!local_pcap_copy
) {
528 /* We have no local cache, wait directly for
529 * async refresh to complete.
531 DEBUG(10,("cups_cache_reload: sync read on fd %d\n",
534 cups_async_callback(smbd_event_context(),
538 if (!local_pcap_copy
) {
542 /* Replace the system cache with our
544 pcap_cache_replace(local_pcap_copy
);
546 DEBUG(10,("cups_cache_reload: async read on fd %d\n",
549 /* Trigger an event when the pipe can be read. */
550 cache_fd_event
= event_add_fd(smbd_event_context(),
555 if (!cache_fd_event
) {
557 TALLOC_FREE(p_pipe_fd
);
565 * 'cups_job_delete()' - Delete a job.
568 static int cups_job_delete(const char *sharename
, const char *lprm_command
, struct printjob
*pjob
)
570 TALLOC_CTX
*frame
= talloc_stackframe();
571 int ret
= 1; /* Return value */
572 http_t
*http
= NULL
; /* HTTP connection to server */
573 ipp_t
*request
= NULL
, /* IPP Request */
574 *response
= NULL
; /* IPP Response */
575 cups_lang_t
*language
= NULL
; /* Default language */
577 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
580 DEBUG(5,("cups_job_delete(%s, %p (%d))\n", sharename
, pjob
, pjob
->sysjob
));
583 * Make sure we don't ask for passwords...
586 cupsSetPasswordCB(cups_passwd_cb
);
589 * Try to connect to the server...
592 if ((http
= cups_connect(frame
)) == NULL
) {
597 * Build an IPP_CANCEL_JOB request, which requires the following
601 * attributes-natural-language
603 * requesting-user-name
608 request
->request
.op
.operation_id
= IPP_CANCEL_JOB
;
609 request
->request
.op
.request_id
= 1;
611 language
= cupsLangDefault();
613 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
614 "attributes-charset", NULL
, "utf-8");
616 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
617 "attributes-natural-language", NULL
, language
->language
);
619 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/jobs/%d", pjob
->sysjob
);
621 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
623 if (push_utf8_talloc(frame
, &user
, pjob
->user
) == (size_t)-1) {
627 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
631 * Do the request and get back a response...
634 if ((response
= cupsDoRequest(http
, request
, "/jobs")) != NULL
) {
635 if (response
->request
.status
.status_code
>= IPP_OK_CONFLICT
) {
636 DEBUG(0,("Unable to cancel job %d - %s\n", pjob
->sysjob
,
637 ippErrorString(cupsLastError())));
642 DEBUG(0,("Unable to cancel job %d - %s\n", pjob
->sysjob
,
643 ippErrorString(cupsLastError())));
651 cupsLangFree(language
);
662 * 'cups_job_pause()' - Pause a job.
665 static int cups_job_pause(int snum
, struct printjob
*pjob
)
667 TALLOC_CTX
*frame
= talloc_stackframe();
668 int ret
= 1; /* Return value */
669 http_t
*http
= NULL
; /* HTTP connection to server */
670 ipp_t
*request
= NULL
, /* IPP Request */
671 *response
= NULL
; /* IPP Response */
672 cups_lang_t
*language
= NULL
; /* Default language */
674 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
677 DEBUG(5,("cups_job_pause(%d, %p (%d))\n", snum
, pjob
, pjob
->sysjob
));
680 * Make sure we don't ask for passwords...
683 cupsSetPasswordCB(cups_passwd_cb
);
686 * Try to connect to the server...
689 if ((http
= cups_connect(frame
)) == NULL
) {
694 * Build an IPP_HOLD_JOB request, which requires the following
698 * attributes-natural-language
700 * requesting-user-name
705 request
->request
.op
.operation_id
= IPP_HOLD_JOB
;
706 request
->request
.op
.request_id
= 1;
708 language
= cupsLangDefault();
710 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
711 "attributes-charset", NULL
, "utf-8");
713 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
714 "attributes-natural-language", NULL
, language
->language
);
716 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/jobs/%d", pjob
->sysjob
);
718 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
720 if (push_utf8_talloc(frame
, &user
, pjob
->user
) == (size_t)-1) {
723 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
727 * Do the request and get back a response...
730 if ((response
= cupsDoRequest(http
, request
, "/jobs")) != NULL
) {
731 if (response
->request
.status
.status_code
>= IPP_OK_CONFLICT
) {
732 DEBUG(0,("Unable to hold job %d - %s\n", pjob
->sysjob
,
733 ippErrorString(cupsLastError())));
738 DEBUG(0,("Unable to hold job %d - %s\n", pjob
->sysjob
,
739 ippErrorString(cupsLastError())));
747 cupsLangFree(language
);
758 * 'cups_job_resume()' - Resume a paused job.
761 static int cups_job_resume(int snum
, struct printjob
*pjob
)
763 TALLOC_CTX
*frame
= talloc_stackframe();
764 int ret
= 1; /* Return value */
765 http_t
*http
= NULL
; /* HTTP connection to server */
766 ipp_t
*request
= NULL
, /* IPP Request */
767 *response
= NULL
; /* IPP Response */
768 cups_lang_t
*language
= NULL
; /* Default language */
770 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
773 DEBUG(5,("cups_job_resume(%d, %p (%d))\n", snum
, pjob
, pjob
->sysjob
));
776 * Make sure we don't ask for passwords...
779 cupsSetPasswordCB(cups_passwd_cb
);
782 * Try to connect to the server...
785 if ((http
= cups_connect(frame
)) == NULL
) {
790 * Build an IPP_RELEASE_JOB request, which requires the following
794 * attributes-natural-language
796 * requesting-user-name
801 request
->request
.op
.operation_id
= IPP_RELEASE_JOB
;
802 request
->request
.op
.request_id
= 1;
804 language
= cupsLangDefault();
806 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
807 "attributes-charset", NULL
, "utf-8");
809 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
810 "attributes-natural-language", NULL
, language
->language
);
812 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/jobs/%d", pjob
->sysjob
);
814 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
816 if (push_utf8_talloc(frame
, &user
, pjob
->user
) == (size_t)-1) {
819 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
823 * Do the request and get back a response...
826 if ((response
= cupsDoRequest(http
, request
, "/jobs")) != NULL
) {
827 if (response
->request
.status
.status_code
>= IPP_OK_CONFLICT
) {
828 DEBUG(0,("Unable to release job %d - %s\n", pjob
->sysjob
,
829 ippErrorString(cupsLastError())));
834 DEBUG(0,("Unable to release job %d - %s\n", pjob
->sysjob
,
835 ippErrorString(cupsLastError())));
843 cupsLangFree(language
);
854 * 'cups_job_submit()' - Submit a job for printing.
857 static int cups_job_submit(int snum
, struct printjob
*pjob
)
859 TALLOC_CTX
*frame
= talloc_stackframe();
860 int ret
= 1; /* Return value */
861 http_t
*http
= NULL
; /* HTTP connection to server */
862 ipp_t
*request
= NULL
, /* IPP Request */
863 *response
= NULL
; /* IPP Response */
864 cups_lang_t
*language
= NULL
; /* Default language */
865 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
866 const char *clientname
= NULL
; /* hostname of client for job-originating-host attribute */
867 char *new_jobname
= NULL
;
869 cups_option_t
*options
= NULL
;
870 char *printername
= NULL
;
872 char *jobname
= NULL
;
873 char *cupsoptions
= NULL
;
874 char *filename
= NULL
;
875 char addr
[INET6_ADDRSTRLEN
];
877 DEBUG(5,("cups_job_submit(%d, %p (%d))\n", snum
, pjob
, pjob
->sysjob
));
880 * Make sure we don't ask for passwords...
883 cupsSetPasswordCB(cups_passwd_cb
);
886 * Try to connect to the server...
889 if ((http
= cups_connect(frame
)) == NULL
) {
894 * Build an IPP_PRINT_JOB request, which requires the following
898 * attributes-natural-language
900 * requesting-user-name
906 request
->request
.op
.operation_id
= IPP_PRINT_JOB
;
907 request
->request
.op
.request_id
= 1;
909 language
= cupsLangDefault();
911 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
912 "attributes-charset", NULL
, "utf-8");
914 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
915 "attributes-natural-language", NULL
, language
->language
);
917 if (push_utf8_talloc(frame
, &printername
, PRINTERNAME(snum
)) == (size_t)-1) {
920 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/printers/%s",
923 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
924 "printer-uri", NULL
, uri
);
926 if (push_utf8_talloc(frame
, &user
, pjob
->user
) == (size_t)-1) {
929 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
932 clientname
= client_name(get_client_fd());
933 if (strcmp(clientname
, "UNKNOWN") == 0) {
934 clientname
= client_addr(get_client_fd(),addr
,sizeof(addr
));
937 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
938 "job-originating-host-name", NULL
,
941 if (push_utf8_talloc(frame
, &jobname
, pjob
->jobname
) == (size_t)-1) {
944 new_jobname
= talloc_asprintf(frame
,
945 "%s%.8u %s", PRINT_SPOOL_PREFIX
,
946 (unsigned int)pjob
->smbjob
,
948 if (new_jobname
== NULL
) {
952 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name", NULL
,
956 * add any options defined in smb.conf
959 if (push_utf8_talloc(frame
, &cupsoptions
, lp_cups_options(snum
)) == (size_t)-1) {
964 num_options
= cupsParseOptions(cupsoptions
, num_options
, &options
);
967 cupsEncodeOptions(request
, num_options
, options
);
970 * Do the request and get back a response...
973 slprintf(uri
, sizeof(uri
) - 1, "/printers/%s", printername
);
975 if (push_utf8_talloc(frame
, &filename
, pjob
->filename
) == (size_t)-1) {
978 if ((response
= cupsDoFileRequest(http
, request
, uri
, pjob
->filename
)) != NULL
) {
979 if (response
->request
.status
.status_code
>= IPP_OK_CONFLICT
) {
980 DEBUG(0,("Unable to print file to %s - %s\n", PRINTERNAME(snum
),
981 ippErrorString(cupsLastError())));
986 DEBUG(0,("Unable to print file to `%s' - %s\n", PRINTERNAME(snum
),
987 ippErrorString(cupsLastError())));
991 unlink(pjob
->filename
);
992 /* else print_job_end will do it for us */
999 cupsLangFree(language
);
1010 * 'cups_queue_get()' - Get all the jobs in the print queue.
1013 static int cups_queue_get(const char *sharename
,
1014 enum printing_types printing_type
,
1016 print_queue_struct
**q
,
1017 print_status_struct
*status
)
1019 TALLOC_CTX
*frame
= talloc_stackframe();
1020 char *printername
= NULL
;
1021 http_t
*http
= NULL
; /* HTTP connection to server */
1022 ipp_t
*request
= NULL
, /* IPP Request */
1023 *response
= NULL
; /* IPP Response */
1024 ipp_attribute_t
*attr
= NULL
; /* Current attribute */
1025 cups_lang_t
*language
= NULL
; /* Default language */
1026 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
1027 int qcount
= 0, /* Number of active queue entries */
1028 qalloc
= 0; /* Number of queue entries allocated */
1029 print_queue_struct
*queue
= NULL
, /* Queue entries */
1030 *temp
; /* Temporary pointer for queue */
1031 char *user_name
= NULL
, /* job-originating-user-name attribute */
1032 *job_name
= NULL
; /* job-name attribute */
1033 int job_id
; /* job-id attribute */
1034 int job_k_octets
; /* job-k-octets attribute */
1035 time_t job_time
; /* time-at-creation attribute */
1036 ipp_jstate_t job_status
; /* job-status attribute */
1037 int job_priority
; /* job-priority attribute */
1038 static const char *jattrs
[] = /* Requested job attributes */
1043 "job-originating-user-name",
1048 static const char *pattrs
[] = /* Requested printer attributes */
1051 "printer-state-message"
1056 /* HACK ALERT!!! The problem with support the 'printer name'
1057 option is that we key the tdb off the sharename. So we will
1058 overload the lpq_command string to pass in the printername
1059 (which is basically what we do for non-cups printers ... using
1060 the lpq_command to get the queue listing). */
1062 if (push_utf8_talloc(frame
, &printername
, lpq_command
) == (size_t)-1) {
1065 DEBUG(5,("cups_queue_get(%s, %p, %p)\n", lpq_command
, q
, status
));
1068 * Make sure we don't ask for passwords...
1071 cupsSetPasswordCB(cups_passwd_cb
);
1074 * Try to connect to the server...
1077 if ((http
= cups_connect(frame
)) == NULL
) {
1082 * Generate the printer URI...
1085 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/printers/%s", printername
);
1088 * Build an IPP_GET_JOBS request, which requires the following
1091 * attributes-charset
1092 * attributes-natural-language
1093 * requested-attributes
1099 request
->request
.op
.operation_id
= IPP_GET_JOBS
;
1100 request
->request
.op
.request_id
= 1;
1102 language
= cupsLangDefault();
1104 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1105 "attributes-charset", NULL
, "utf-8");
1107 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1108 "attributes-natural-language", NULL
, language
->language
);
1110 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1111 "requested-attributes",
1112 (sizeof(jattrs
) / sizeof(jattrs
[0])),
1115 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1116 "printer-uri", NULL
, uri
);
1119 * Do the request and get back a response...
1122 if ((response
= cupsDoRequest(http
, request
, "/")) == NULL
) {
1123 DEBUG(0,("Unable to get jobs for %s - %s\n", uri
,
1124 ippErrorString(cupsLastError())));
1128 if (response
->request
.status
.status_code
>= IPP_OK_CONFLICT
) {
1129 DEBUG(0,("Unable to get jobs for %s - %s\n", uri
,
1130 ippErrorString(response
->request
.status
.status_code
)));
1135 * Process the jobs...
1142 for (attr
= response
->attrs
; attr
!= NULL
; attr
= attr
->next
) {
1144 * Skip leading attributes until we hit a job...
1147 while (attr
!= NULL
&& attr
->group_tag
!= IPP_TAG_JOB
)
1154 * Allocate memory as needed...
1156 if (qcount
>= qalloc
) {
1159 queue
= SMB_REALLOC_ARRAY(queue
, print_queue_struct
, qalloc
);
1161 if (queue
== NULL
) {
1162 DEBUG(0,("cups_queue_get: Not enough memory!"));
1168 temp
= queue
+ qcount
;
1169 memset(temp
, 0, sizeof(print_queue_struct
));
1172 * Pull the needed attributes from this job...
1177 job_status
= IPP_JOB_PENDING
;
1183 while (attr
!= NULL
&& attr
->group_tag
== IPP_TAG_JOB
) {
1184 if (attr
->name
== NULL
) {
1189 if (strcmp(attr
->name
, "job-id") == 0 &&
1190 attr
->value_tag
== IPP_TAG_INTEGER
)
1191 job_id
= attr
->values
[0].integer
;
1193 if (strcmp(attr
->name
, "job-k-octets") == 0 &&
1194 attr
->value_tag
== IPP_TAG_INTEGER
)
1195 job_k_octets
= attr
->values
[0].integer
;
1197 if (strcmp(attr
->name
, "job-priority") == 0 &&
1198 attr
->value_tag
== IPP_TAG_INTEGER
)
1199 job_priority
= attr
->values
[0].integer
;
1201 if (strcmp(attr
->name
, "job-state") == 0 &&
1202 attr
->value_tag
== IPP_TAG_ENUM
)
1203 job_status
= (ipp_jstate_t
)(attr
->values
[0].integer
);
1205 if (strcmp(attr
->name
, "time-at-creation") == 0 &&
1206 attr
->value_tag
== IPP_TAG_INTEGER
)
1207 job_time
= attr
->values
[0].integer
;
1209 if (strcmp(attr
->name
, "job-name") == 0 &&
1210 attr
->value_tag
== IPP_TAG_NAME
) {
1211 pull_utf8_talloc(frame
,
1213 attr
->values
[0].string
.text
);
1216 if (strcmp(attr
->name
, "job-originating-user-name") == 0 &&
1217 attr
->value_tag
== IPP_TAG_NAME
) {
1218 pull_utf8_talloc(frame
,
1220 attr
->values
[0].string
.text
);
1227 * See if we have everything needed...
1230 if (user_name
== NULL
|| job_name
== NULL
|| job_id
== 0) {
1238 temp
->size
= job_k_octets
* 1024;
1239 temp
->status
= job_status
== IPP_JOB_PENDING
? LPQ_QUEUED
:
1240 job_status
== IPP_JOB_STOPPED
? LPQ_PAUSED
:
1241 job_status
== IPP_JOB_HELD
? LPQ_PAUSED
:
1243 temp
->priority
= job_priority
;
1244 temp
->time
= job_time
;
1245 strlcpy(temp
->fs_user
, user_name
, sizeof(temp
->fs_user
));
1246 strlcpy(temp
->fs_file
, job_name
, sizeof(temp
->fs_file
));
1254 ippDelete(response
);
1258 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the
1259 * following attributes:
1261 * attributes-charset
1262 * attributes-natural-language
1263 * requested-attributes
1269 request
->request
.op
.operation_id
= IPP_GET_PRINTER_ATTRIBUTES
;
1270 request
->request
.op
.request_id
= 1;
1272 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1273 "attributes-charset", NULL
, "utf-8");
1275 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1276 "attributes-natural-language", NULL
, language
->language
);
1278 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1279 "requested-attributes",
1280 (sizeof(pattrs
) / sizeof(pattrs
[0])),
1283 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1284 "printer-uri", NULL
, uri
);
1287 * Do the request and get back a response...
1290 if ((response
= cupsDoRequest(http
, request
, "/")) == NULL
) {
1291 DEBUG(0,("Unable to get printer status for %s - %s\n", printername
,
1292 ippErrorString(cupsLastError())));
1297 if (response
->request
.status
.status_code
>= IPP_OK_CONFLICT
) {
1298 DEBUG(0,("Unable to get printer status for %s - %s\n", printername
,
1299 ippErrorString(response
->request
.status
.status_code
)));
1305 * Get the current printer status and convert it to the SAMBA values.
1308 if ((attr
= ippFindAttribute(response
, "printer-state", IPP_TAG_ENUM
)) != NULL
) {
1309 if (attr
->values
[0].integer
== IPP_PRINTER_STOPPED
)
1310 status
->status
= LPSTAT_STOPPED
;
1312 status
->status
= LPSTAT_OK
;
1315 if ((attr
= ippFindAttribute(response
, "printer-state-message",
1316 IPP_TAG_TEXT
)) != NULL
) {
1318 pull_utf8_talloc(frame
, &msg
, attr
->values
[0].string
.text
);
1319 fstrcpy(status
->message
, msg
);
1323 * Return the job queue...
1330 ippDelete(response
);
1333 cupsLangFree(language
);
1344 * 'cups_queue_pause()' - Pause a print queue.
1347 static int cups_queue_pause(int snum
)
1349 TALLOC_CTX
*frame
= talloc_stackframe();
1350 int ret
= 1; /* Return value */
1351 http_t
*http
= NULL
; /* HTTP connection to server */
1352 ipp_t
*request
= NULL
, /* IPP Request */
1353 *response
= NULL
; /* IPP Response */
1354 cups_lang_t
*language
= NULL
; /* Default language */
1355 char *printername
= NULL
;
1356 char *username
= NULL
;
1357 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
1360 DEBUG(5,("cups_queue_pause(%d)\n", snum
));
1363 * Make sure we don't ask for passwords...
1366 cupsSetPasswordCB(cups_passwd_cb
);
1369 * Try to connect to the server...
1372 if ((http
= cups_connect(frame
)) == NULL
) {
1377 * Build an IPP_PAUSE_PRINTER request, which requires the following
1380 * attributes-charset
1381 * attributes-natural-language
1383 * requesting-user-name
1388 request
->request
.op
.operation_id
= IPP_PAUSE_PRINTER
;
1389 request
->request
.op
.request_id
= 1;
1391 language
= cupsLangDefault();
1393 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1394 "attributes-charset", NULL
, "utf-8");
1396 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1397 "attributes-natural-language", NULL
, language
->language
);
1399 if (push_utf8_talloc(frame
, &printername
, PRINTERNAME(snum
)) == (size_t)-1) {
1402 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/printers/%s",
1405 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
1407 if (push_utf8_talloc(frame
, &username
, current_user_info
.unix_name
) == (size_t)-1) {
1410 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
1414 * Do the request and get back a response...
1417 if ((response
= cupsDoRequest(http
, request
, "/admin/")) != NULL
) {
1418 if (response
->request
.status
.status_code
>= IPP_OK_CONFLICT
) {
1419 DEBUG(0,("Unable to pause printer %s - %s\n", PRINTERNAME(snum
),
1420 ippErrorString(cupsLastError())));
1425 DEBUG(0,("Unable to pause printer %s - %s\n", PRINTERNAME(snum
),
1426 ippErrorString(cupsLastError())));
1431 ippDelete(response
);
1434 cupsLangFree(language
);
1445 * 'cups_queue_resume()' - Restart a print queue.
1448 static int cups_queue_resume(int snum
)
1450 TALLOC_CTX
*frame
= talloc_stackframe();
1451 int ret
= 1; /* Return value */
1452 http_t
*http
= NULL
; /* HTTP connection to server */
1453 ipp_t
*request
= NULL
, /* IPP Request */
1454 *response
= NULL
; /* IPP Response */
1455 cups_lang_t
*language
= NULL
; /* Default language */
1456 char *printername
= NULL
;
1457 char *username
= NULL
;
1458 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
1461 DEBUG(5,("cups_queue_resume(%d)\n", snum
));
1464 * Make sure we don't ask for passwords...
1467 cupsSetPasswordCB(cups_passwd_cb
);
1470 * Try to connect to the server...
1473 if ((http
= cups_connect(frame
)) == NULL
) {
1478 * Build an IPP_RESUME_PRINTER request, which requires the following
1481 * attributes-charset
1482 * attributes-natural-language
1484 * requesting-user-name
1489 request
->request
.op
.operation_id
= IPP_RESUME_PRINTER
;
1490 request
->request
.op
.request_id
= 1;
1492 language
= cupsLangDefault();
1494 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1495 "attributes-charset", NULL
, "utf-8");
1497 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1498 "attributes-natural-language", NULL
, language
->language
);
1500 if (push_utf8_talloc(frame
, &printername
, PRINTERNAME(snum
)) == (size_t)-1) {
1503 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/printers/%s",
1506 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
1508 if (push_utf8_talloc(frame
, &username
, current_user_info
.unix_name
) == (size_t)-1) {
1511 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
1515 * Do the request and get back a response...
1518 if ((response
= cupsDoRequest(http
, request
, "/admin/")) != NULL
) {
1519 if (response
->request
.status
.status_code
>= IPP_OK_CONFLICT
) {
1520 DEBUG(0,("Unable to resume printer %s - %s\n", PRINTERNAME(snum
),
1521 ippErrorString(cupsLastError())));
1526 DEBUG(0,("Unable to resume printer %s - %s\n", PRINTERNAME(snum
),
1527 ippErrorString(cupsLastError())));
1532 ippDelete(response
);
1535 cupsLangFree(language
);
1544 /*******************************************************************
1545 * CUPS printing interface definitions...
1546 ******************************************************************/
1548 struct printif cups_printif
=
1560 bool cups_pull_comment_location(NT_PRINTER_INFO_LEVEL_2
*printer
)
1562 TALLOC_CTX
*frame
= talloc_stackframe();
1563 http_t
*http
= NULL
; /* HTTP connection to server */
1564 ipp_t
*request
= NULL
, /* IPP Request */
1565 *response
= NULL
; /* IPP Response */
1566 ipp_attribute_t
*attr
; /* Current attribute */
1567 cups_lang_t
*language
= NULL
; /* Default language */
1568 char uri
[HTTP_MAX_URI
];
1569 char *server
= NULL
;
1570 char *sharename
= NULL
;
1572 static const char *requested
[] =/* Requested attributes */
1580 DEBUG(5, ("pulling %s location\n", printer
->sharename
));
1583 * Make sure we don't ask for passwords...
1586 cupsSetPasswordCB(cups_passwd_cb
);
1589 * Try to connect to the server...
1592 if ((http
= cups_connect(frame
)) == NULL
) {
1598 request
->request
.op
.operation_id
= IPP_GET_PRINTER_ATTRIBUTES
;
1599 request
->request
.op
.request_id
= 1;
1601 language
= cupsLangDefault();
1603 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1604 "attributes-charset", NULL
, "utf-8");
1606 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1607 "attributes-natural-language", NULL
, language
->language
);
1609 if (lp_cups_server() != NULL
&& strlen(lp_cups_server()) > 0) {
1610 if (push_utf8_talloc(frame
, &server
, lp_cups_server()) == (size_t)-1) {
1614 server
= talloc_strdup(frame
,cupsServer());
1619 if (push_utf8_talloc(frame
, &sharename
, printer
->sharename
) == (size_t)-1) {
1622 slprintf(uri
, sizeof(uri
) - 1, "ipp://%s/printers/%s",
1625 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1626 "printer-uri", NULL
, uri
);
1628 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1629 "requested-attributes",
1630 (sizeof(requested
) / sizeof(requested
[0])),
1634 * Do the request and get back a response...
1637 if ((response
= cupsDoRequest(http
, request
, "/")) == NULL
) {
1638 DEBUG(0,("Unable to get printer attributes - %s\n",
1639 ippErrorString(cupsLastError())));
1643 for (attr
= response
->attrs
; attr
!= NULL
;) {
1645 * Skip leading attributes until we hit a printer...
1648 while (attr
!= NULL
&& attr
->group_tag
!= IPP_TAG_PRINTER
)
1655 * Pull the needed attributes from this printer...
1658 while ( attr
&& (attr
->group_tag
== IPP_TAG_PRINTER
) ) {
1659 if (strcmp(attr
->name
, "printer-name") == 0 &&
1660 attr
->value_tag
== IPP_TAG_NAME
) {
1661 pull_utf8_talloc(frame
,
1663 attr
->values
[0].string
.text
);
1666 /* Grab the comment if we don't have one */
1667 if ( (strcmp(attr
->name
, "printer-info") == 0)
1668 && (attr
->value_tag
== IPP_TAG_TEXT
)
1669 && !strlen(printer
->comment
) )
1671 char *comment
= NULL
;
1672 pull_utf8_talloc(frame
,
1674 attr
->values
[0].string
.text
);
1675 DEBUG(5,("cups_pull_comment_location: Using cups comment: %s\n",
1677 strlcpy(printer
->comment
,
1679 sizeof(printer
->comment
));
1682 /* Grab the location if we don't have one */
1683 if ( (strcmp(attr
->name
, "printer-location") == 0)
1684 && (attr
->value_tag
== IPP_TAG_TEXT
)
1685 && !strlen(printer
->location
) )
1687 char *location
= NULL
;
1688 pull_utf8_talloc(frame
,
1690 attr
->values
[0].string
.text
);
1691 DEBUG(5,("cups_pull_comment_location: Using cups location: %s\n",
1693 strlcpy(printer
->location
,
1695 sizeof(printer
->location
));
1702 * We have everything needed...
1713 ippDelete(response
);
1720 cupsLangFree(language
);
1730 /* this keeps fussy compilers happy */
1731 void print_cups_dummy(void);
1732 void print_cups_dummy(void) {}
1733 #endif /* HAVE_CUPS */