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 http
= httpConnect(server
, port
);
98 CatchSignal(SIGALRM
, SIGNAL_CAST SIG_IGN
);
102 DEBUG(0,("Unable to connect to CUPS server %s:%d - %s\n",
103 server
, port
, strerror(errno
)));
109 static void send_pcap_info(const char *name
, const char *info
, void *pd
)
112 size_t namelen
= name
? strlen(name
)+1 : 0;
113 size_t infolen
= info
? strlen(info
)+1 : 0;
115 DEBUG(11,("send_pcap_info: writing namelen %u\n", (unsigned int)namelen
));
116 if (sys_write(fd
, &namelen
, sizeof(namelen
)) != sizeof(namelen
)) {
117 DEBUG(10,("send_pcap_info: namelen write failed %s\n",
121 DEBUG(11,("send_pcap_info: writing infolen %u\n", (unsigned int)infolen
));
122 if (sys_write(fd
, &infolen
, sizeof(infolen
)) != sizeof(infolen
)) {
123 DEBUG(10,("send_pcap_info: infolen write failed %s\n",
128 DEBUG(11,("send_pcap_info: writing name %s\n", name
));
129 if (sys_write(fd
, name
, namelen
) != namelen
) {
130 DEBUG(10,("send_pcap_info: name write failed %s\n",
136 DEBUG(11,("send_pcap_info: writing info %s\n", info
));
137 if (sys_write(fd
, info
, infolen
) != infolen
) {
138 DEBUG(10,("send_pcap_info: info write failed %s\n",
145 static bool cups_cache_reload_async(int fd
)
147 TALLOC_CTX
*frame
= talloc_stackframe();
148 struct pcap_cache
*tmp_pcap_cache
= NULL
;
149 http_t
*http
= NULL
; /* HTTP connection to server */
150 ipp_t
*request
= NULL
, /* IPP Request */
151 *response
= NULL
; /* IPP Response */
152 ipp_attribute_t
*attr
; /* Current attribute */
153 cups_lang_t
*language
= NULL
; /* Default language */
154 char *name
, /* printer-name attribute */
155 *info
; /* printer-info attribute */
156 static const char *requested
[] =/* Requested attributes */
164 DEBUG(5, ("reloading cups printcap cache\n"));
167 * Make sure we don't ask for passwords...
170 cupsSetPasswordCB(cups_passwd_cb
);
173 * Try to connect to the server...
176 if ((http
= cups_connect(frame
)) == NULL
) {
181 * Build a CUPS_GET_PRINTERS request, which requires the following
185 * attributes-natural-language
186 * requested-attributes
191 request
->request
.op
.operation_id
= CUPS_GET_PRINTERS
;
192 request
->request
.op
.request_id
= 1;
194 language
= cupsLangDefault();
196 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
197 "attributes-charset", NULL
, "utf-8");
199 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
200 "attributes-natural-language", NULL
, language
->language
);
202 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
203 "requested-attributes",
204 (sizeof(requested
) / sizeof(requested
[0])),
208 * Do the request and get back a response...
211 if ((response
= cupsDoRequest(http
, request
, "/")) == NULL
) {
212 DEBUG(0,("Unable to get printer list - %s\n",
213 ippErrorString(cupsLastError())));
217 for (attr
= response
->attrs
; attr
!= NULL
;) {
219 * Skip leading attributes until we hit a printer...
222 while (attr
!= NULL
&& attr
->group_tag
!= IPP_TAG_PRINTER
)
229 * Pull the needed attributes from this printer...
235 while (attr
!= NULL
&& attr
->group_tag
== IPP_TAG_PRINTER
) {
236 if (strcmp(attr
->name
, "printer-name") == 0 &&
237 attr
->value_tag
== IPP_TAG_NAME
) {
238 if (!pull_utf8_talloc(frame
,
240 attr
->values
[0].string
.text
,
246 if (strcmp(attr
->name
, "printer-info") == 0 &&
247 attr
->value_tag
== IPP_TAG_TEXT
) {
248 if (!pull_utf8_talloc(frame
,
250 attr
->values
[0].string
.text
,
260 * See if we have everything needed...
266 if (!pcap_cache_add_specific(&tmp_pcap_cache
, name
, info
)) {
275 * Build a CUPS_GET_CLASSES request, which requires the following
279 * attributes-natural-language
280 * requested-attributes
285 request
->request
.op
.operation_id
= CUPS_GET_CLASSES
;
286 request
->request
.op
.request_id
= 1;
288 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
289 "attributes-charset", NULL
, "utf-8");
291 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
292 "attributes-natural-language", NULL
, language
->language
);
294 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
295 "requested-attributes",
296 (sizeof(requested
) / sizeof(requested
[0])),
300 * Do the request and get back a response...
303 if ((response
= cupsDoRequest(http
, request
, "/")) == NULL
) {
304 DEBUG(0,("Unable to get printer list - %s\n",
305 ippErrorString(cupsLastError())));
309 for (attr
= response
->attrs
; attr
!= NULL
;) {
311 * Skip leading attributes until we hit a printer...
314 while (attr
!= NULL
&& attr
->group_tag
!= IPP_TAG_PRINTER
)
321 * Pull the needed attributes from this printer...
327 while (attr
!= NULL
&& attr
->group_tag
== IPP_TAG_PRINTER
) {
328 if (strcmp(attr
->name
, "printer-name") == 0 &&
329 attr
->value_tag
== IPP_TAG_NAME
) {
330 if (!pull_utf8_talloc(frame
,
332 attr
->values
[0].string
.text
,
338 if (strcmp(attr
->name
, "printer-info") == 0 &&
339 attr
->value_tag
== IPP_TAG_TEXT
) {
340 if (!pull_utf8_talloc(frame
,
342 attr
->values
[0].string
.text
,
352 * See if we have everything needed...
358 if (!pcap_cache_add_specific(&tmp_pcap_cache
, name
, info
)) {
370 cupsLangFree(language
);
375 /* Send all the entries up the pipe. */
376 if (tmp_pcap_cache
) {
377 pcap_printer_fn_specific(tmp_pcap_cache
,
381 pcap_cache_destroy_specific(&tmp_pcap_cache
);
387 static struct pcap_cache
*local_pcap_copy
;
388 struct fd_event
*cache_fd_event
;
390 static bool cups_pcap_load_async(int *pfd
)
397 if (cache_fd_event
) {
398 DEBUG(3,("cups_pcap_load_async: already waiting for "
399 "a refresh event\n" ));
403 DEBUG(5,("cups_pcap_load_async: asynchronously loading cups printers\n"));
405 if (pipe(fds
) == -1) {
410 if (pid
== (pid_t
)-1) {
411 DEBUG(10,("cups_pcap_load_async: fork failed %s\n",
419 DEBUG(10,("cups_pcap_load_async: child pid = %u\n",
420 (unsigned int)pid
));
429 cups_cache_reload_async(fds
[1]);
434 static void cups_async_callback(struct event_context
*event_ctx
,
435 struct fd_event
*event
,
439 TALLOC_CTX
*frame
= talloc_stackframe();
441 struct pcap_cache
*tmp_pcap_cache
= NULL
;
443 DEBUG(5,("cups_async_callback: callback received for printer data. "
447 char *name
= NULL
, *info
= NULL
;
448 size_t namelen
= 0, infolen
= 0;
451 ret
= sys_read(fd
, &namelen
, sizeof(namelen
));
456 if (ret
!= sizeof(namelen
)) {
457 DEBUG(10,("cups_async_callback: namelen read failed %d %s\n",
458 errno
, strerror(errno
)));
462 DEBUG(11,("cups_async_callback: read namelen %u\n",
463 (unsigned int)namelen
));
465 ret
= sys_read(fd
, &infolen
, sizeof(infolen
));
470 if (ret
!= sizeof(infolen
)) {
471 DEBUG(10,("cups_async_callback: infolen read failed %s\n",
476 DEBUG(11,("cups_async_callback: read infolen %u\n",
477 (unsigned int)infolen
));
480 name
= TALLOC_ARRAY(frame
, char, namelen
);
484 ret
= sys_read(fd
, name
, namelen
);
489 if (ret
!= namelen
) {
490 DEBUG(10,("cups_async_callback: name read failed %s\n",
494 DEBUG(11,("cups_async_callback: read name %s\n",
500 info
= TALLOC_ARRAY(frame
, char, infolen
);
504 ret
= sys_read(fd
, info
, infolen
);
509 if (ret
!= infolen
) {
510 DEBUG(10,("cups_async_callback: info read failed %s\n",
514 DEBUG(11,("cups_async_callback: read info %s\n",
520 /* Add to our local pcap cache. */
521 pcap_cache_add_specific(&tmp_pcap_cache
, name
, info
);
527 if (tmp_pcap_cache
) {
528 /* We got a namelist, replace our local cache. */
529 pcap_cache_destroy_specific(&local_pcap_copy
);
530 local_pcap_copy
= tmp_pcap_cache
;
532 /* And the systemwide pcap cache. */
533 pcap_cache_replace(local_pcap_copy
);
535 DEBUG(2,("cups_async_callback: failed to read a new "
540 TALLOC_FREE(cache_fd_event
);
543 bool cups_cache_reload(void)
545 int *p_pipe_fd
= TALLOC_P(NULL
, int);
551 /* Set up an async refresh. */
552 if (!cups_pcap_load_async(p_pipe_fd
)) {
555 if (!local_pcap_copy
) {
556 /* We have no local cache, wait directly for
557 * async refresh to complete.
559 DEBUG(10,("cups_cache_reload: sync read on fd %d\n",
562 cups_async_callback(smbd_event_context(),
566 if (!local_pcap_copy
) {
570 /* Replace the system cache with our
572 pcap_cache_replace(local_pcap_copy
);
574 DEBUG(10,("cups_cache_reload: async read on fd %d\n",
577 /* Trigger an event when the pipe can be read. */
578 cache_fd_event
= event_add_fd(smbd_event_context(),
583 if (!cache_fd_event
) {
585 TALLOC_FREE(p_pipe_fd
);
593 * 'cups_job_delete()' - Delete a job.
596 static int cups_job_delete(const char *sharename
, const char *lprm_command
, struct printjob
*pjob
)
598 TALLOC_CTX
*frame
= talloc_stackframe();
599 int ret
= 1; /* Return value */
600 http_t
*http
= NULL
; /* HTTP connection to server */
601 ipp_t
*request
= NULL
, /* IPP Request */
602 *response
= NULL
; /* IPP Response */
603 cups_lang_t
*language
= NULL
; /* Default language */
605 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
608 DEBUG(5,("cups_job_delete(%s, %p (%d))\n", sharename
, pjob
, pjob
->sysjob
));
611 * Make sure we don't ask for passwords...
614 cupsSetPasswordCB(cups_passwd_cb
);
617 * Try to connect to the server...
620 if ((http
= cups_connect(frame
)) == NULL
) {
625 * Build an IPP_CANCEL_JOB request, which requires the following
629 * attributes-natural-language
631 * requesting-user-name
636 request
->request
.op
.operation_id
= IPP_CANCEL_JOB
;
637 request
->request
.op
.request_id
= 1;
639 language
= cupsLangDefault();
641 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
642 "attributes-charset", NULL
, "utf-8");
644 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
645 "attributes-natural-language", NULL
, language
->language
);
647 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/jobs/%d", pjob
->sysjob
);
649 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
651 if (!push_utf8_talloc(frame
, &user
, pjob
->user
, &size
)) {
655 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
659 * Do the request and get back a response...
662 if ((response
= cupsDoRequest(http
, request
, "/jobs")) != NULL
) {
663 if (response
->request
.status
.status_code
>= IPP_OK_CONFLICT
) {
664 DEBUG(0,("Unable to cancel job %d - %s\n", pjob
->sysjob
,
665 ippErrorString(cupsLastError())));
670 DEBUG(0,("Unable to cancel job %d - %s\n", pjob
->sysjob
,
671 ippErrorString(cupsLastError())));
679 cupsLangFree(language
);
690 * 'cups_job_pause()' - Pause a job.
693 static int cups_job_pause(int snum
, struct printjob
*pjob
)
695 TALLOC_CTX
*frame
= talloc_stackframe();
696 int ret
= 1; /* Return value */
697 http_t
*http
= NULL
; /* HTTP connection to server */
698 ipp_t
*request
= NULL
, /* IPP Request */
699 *response
= NULL
; /* IPP Response */
700 cups_lang_t
*language
= NULL
; /* Default language */
702 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
705 DEBUG(5,("cups_job_pause(%d, %p (%d))\n", snum
, pjob
, pjob
->sysjob
));
708 * Make sure we don't ask for passwords...
711 cupsSetPasswordCB(cups_passwd_cb
);
714 * Try to connect to the server...
717 if ((http
= cups_connect(frame
)) == NULL
) {
722 * Build an IPP_HOLD_JOB request, which requires the following
726 * attributes-natural-language
728 * requesting-user-name
733 request
->request
.op
.operation_id
= IPP_HOLD_JOB
;
734 request
->request
.op
.request_id
= 1;
736 language
= cupsLangDefault();
738 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
739 "attributes-charset", NULL
, "utf-8");
741 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
742 "attributes-natural-language", NULL
, language
->language
);
744 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/jobs/%d", pjob
->sysjob
);
746 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
748 if (!push_utf8_talloc(frame
, &user
, pjob
->user
, &size
)) {
751 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
755 * Do the request and get back a response...
758 if ((response
= cupsDoRequest(http
, request
, "/jobs")) != NULL
) {
759 if (response
->request
.status
.status_code
>= IPP_OK_CONFLICT
) {
760 DEBUG(0,("Unable to hold job %d - %s\n", pjob
->sysjob
,
761 ippErrorString(cupsLastError())));
766 DEBUG(0,("Unable to hold job %d - %s\n", pjob
->sysjob
,
767 ippErrorString(cupsLastError())));
775 cupsLangFree(language
);
786 * 'cups_job_resume()' - Resume a paused job.
789 static int cups_job_resume(int snum
, struct printjob
*pjob
)
791 TALLOC_CTX
*frame
= talloc_stackframe();
792 int ret
= 1; /* Return value */
793 http_t
*http
= NULL
; /* HTTP connection to server */
794 ipp_t
*request
= NULL
, /* IPP Request */
795 *response
= NULL
; /* IPP Response */
796 cups_lang_t
*language
= NULL
; /* Default language */
798 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
801 DEBUG(5,("cups_job_resume(%d, %p (%d))\n", snum
, pjob
, pjob
->sysjob
));
804 * Make sure we don't ask for passwords...
807 cupsSetPasswordCB(cups_passwd_cb
);
810 * Try to connect to the server...
813 if ((http
= cups_connect(frame
)) == NULL
) {
818 * Build an IPP_RELEASE_JOB request, which requires the following
822 * attributes-natural-language
824 * requesting-user-name
829 request
->request
.op
.operation_id
= IPP_RELEASE_JOB
;
830 request
->request
.op
.request_id
= 1;
832 language
= cupsLangDefault();
834 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
835 "attributes-charset", NULL
, "utf-8");
837 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
838 "attributes-natural-language", NULL
, language
->language
);
840 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/jobs/%d", pjob
->sysjob
);
842 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
844 if (!push_utf8_talloc(frame
, &user
, pjob
->user
, &size
)) {
847 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
851 * Do the request and get back a response...
854 if ((response
= cupsDoRequest(http
, request
, "/jobs")) != NULL
) {
855 if (response
->request
.status
.status_code
>= IPP_OK_CONFLICT
) {
856 DEBUG(0,("Unable to release job %d - %s\n", pjob
->sysjob
,
857 ippErrorString(cupsLastError())));
862 DEBUG(0,("Unable to release job %d - %s\n", pjob
->sysjob
,
863 ippErrorString(cupsLastError())));
871 cupsLangFree(language
);
882 * 'cups_job_submit()' - Submit a job for printing.
885 static int cups_job_submit(int snum
, struct printjob
*pjob
)
887 TALLOC_CTX
*frame
= talloc_stackframe();
888 int ret
= 1; /* Return value */
889 http_t
*http
= NULL
; /* HTTP connection to server */
890 ipp_t
*request
= NULL
, /* IPP Request */
891 *response
= NULL
; /* IPP Response */
892 cups_lang_t
*language
= NULL
; /* Default language */
893 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
894 const char *clientname
= NULL
; /* hostname of client for job-originating-host attribute */
895 char *new_jobname
= NULL
;
897 cups_option_t
*options
= NULL
;
898 char *printername
= NULL
;
900 char *jobname
= NULL
;
901 char *cupsoptions
= NULL
;
902 char *filename
= NULL
;
904 char addr
[INET6_ADDRSTRLEN
];
906 DEBUG(5,("cups_job_submit(%d, %p (%d))\n", snum
, pjob
, pjob
->sysjob
));
909 * Make sure we don't ask for passwords...
912 cupsSetPasswordCB(cups_passwd_cb
);
915 * Try to connect to the server...
918 if ((http
= cups_connect(frame
)) == NULL
) {
923 * Build an IPP_PRINT_JOB request, which requires the following
927 * attributes-natural-language
929 * requesting-user-name
935 request
->request
.op
.operation_id
= IPP_PRINT_JOB
;
936 request
->request
.op
.request_id
= 1;
938 language
= cupsLangDefault();
940 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
941 "attributes-charset", NULL
, "utf-8");
943 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
944 "attributes-natural-language", NULL
, language
->language
);
946 if (!push_utf8_talloc(frame
, &printername
, PRINTERNAME(snum
), &size
)) {
949 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/printers/%s",
952 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
953 "printer-uri", NULL
, uri
);
955 if (!push_utf8_talloc(frame
, &user
, pjob
->user
, &size
)) {
958 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
961 clientname
= client_name(get_client_fd());
962 if (strcmp(clientname
, "UNKNOWN") == 0) {
963 clientname
= client_addr(get_client_fd(),addr
,sizeof(addr
));
966 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
967 "job-originating-host-name", NULL
,
970 if (!push_utf8_talloc(frame
, &jobname
, pjob
->jobname
, &size
)) {
973 new_jobname
= talloc_asprintf(frame
,
974 "%s%.8u %s", PRINT_SPOOL_PREFIX
,
975 (unsigned int)pjob
->smbjob
,
977 if (new_jobname
== NULL
) {
981 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name", NULL
,
985 * add any options defined in smb.conf
988 if (!push_utf8_talloc(frame
, &cupsoptions
, lp_cups_options(snum
), &size
)) {
993 num_options
= cupsParseOptions(cupsoptions
, num_options
, &options
);
996 cupsEncodeOptions(request
, num_options
, options
);
999 * Do the request and get back a response...
1002 slprintf(uri
, sizeof(uri
) - 1, "/printers/%s", printername
);
1004 if (!push_utf8_talloc(frame
, &filename
, pjob
->filename
, &size
)) {
1007 if ((response
= cupsDoFileRequest(http
, request
, uri
, pjob
->filename
)) != NULL
) {
1008 if (response
->request
.status
.status_code
>= IPP_OK_CONFLICT
) {
1009 DEBUG(0,("Unable to print file to %s - %s\n", PRINTERNAME(snum
),
1010 ippErrorString(cupsLastError())));
1015 DEBUG(0,("Unable to print file to `%s' - %s\n", PRINTERNAME(snum
),
1016 ippErrorString(cupsLastError())));
1020 unlink(pjob
->filename
);
1021 /* else print_job_end will do it for us */
1025 ippDelete(response
);
1028 cupsLangFree(language
);
1039 * 'cups_queue_get()' - Get all the jobs in the print queue.
1042 static int cups_queue_get(const char *sharename
,
1043 enum printing_types printing_type
,
1045 print_queue_struct
**q
,
1046 print_status_struct
*status
)
1048 TALLOC_CTX
*frame
= talloc_stackframe();
1049 char *printername
= NULL
;
1050 http_t
*http
= NULL
; /* HTTP connection to server */
1051 ipp_t
*request
= NULL
, /* IPP Request */
1052 *response
= NULL
; /* IPP Response */
1053 ipp_attribute_t
*attr
= NULL
; /* Current attribute */
1054 cups_lang_t
*language
= NULL
; /* Default language */
1055 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
1056 int qcount
= 0, /* Number of active queue entries */
1057 qalloc
= 0; /* Number of queue entries allocated */
1058 print_queue_struct
*queue
= NULL
, /* Queue entries */
1059 *temp
; /* Temporary pointer for queue */
1060 char *user_name
= NULL
, /* job-originating-user-name attribute */
1061 *job_name
= NULL
; /* job-name attribute */
1062 int job_id
; /* job-id attribute */
1063 int job_k_octets
; /* job-k-octets attribute */
1064 time_t job_time
; /* time-at-creation attribute */
1065 ipp_jstate_t job_status
; /* job-status attribute */
1066 int job_priority
; /* job-priority attribute */
1068 static const char *jattrs
[] = /* Requested job attributes */
1073 "job-originating-user-name",
1078 static const char *pattrs
[] = /* Requested printer attributes */
1081 "printer-state-message"
1086 /* HACK ALERT!!! The problem with support the 'printer name'
1087 option is that we key the tdb off the sharename. So we will
1088 overload the lpq_command string to pass in the printername
1089 (which is basically what we do for non-cups printers ... using
1090 the lpq_command to get the queue listing). */
1092 if (!push_utf8_talloc(frame
, &printername
, lpq_command
, &size
)) {
1095 DEBUG(5,("cups_queue_get(%s, %p, %p)\n", lpq_command
, q
, status
));
1098 * Make sure we don't ask for passwords...
1101 cupsSetPasswordCB(cups_passwd_cb
);
1104 * Try to connect to the server...
1107 if ((http
= cups_connect(frame
)) == NULL
) {
1112 * Generate the printer URI...
1115 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/printers/%s", printername
);
1118 * Build an IPP_GET_JOBS request, which requires the following
1121 * attributes-charset
1122 * attributes-natural-language
1123 * requested-attributes
1129 request
->request
.op
.operation_id
= IPP_GET_JOBS
;
1130 request
->request
.op
.request_id
= 1;
1132 language
= cupsLangDefault();
1134 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1135 "attributes-charset", NULL
, "utf-8");
1137 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1138 "attributes-natural-language", NULL
, language
->language
);
1140 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1141 "requested-attributes",
1142 (sizeof(jattrs
) / sizeof(jattrs
[0])),
1145 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1146 "printer-uri", NULL
, uri
);
1149 * Do the request and get back a response...
1152 if ((response
= cupsDoRequest(http
, request
, "/")) == NULL
) {
1153 DEBUG(0,("Unable to get jobs for %s - %s\n", uri
,
1154 ippErrorString(cupsLastError())));
1158 if (response
->request
.status
.status_code
>= IPP_OK_CONFLICT
) {
1159 DEBUG(0,("Unable to get jobs for %s - %s\n", uri
,
1160 ippErrorString(response
->request
.status
.status_code
)));
1165 * Process the jobs...
1172 for (attr
= response
->attrs
; attr
!= NULL
; attr
= attr
->next
) {
1174 * Skip leading attributes until we hit a job...
1177 while (attr
!= NULL
&& attr
->group_tag
!= IPP_TAG_JOB
)
1184 * Allocate memory as needed...
1186 if (qcount
>= qalloc
) {
1189 queue
= SMB_REALLOC_ARRAY(queue
, print_queue_struct
, qalloc
);
1191 if (queue
== NULL
) {
1192 DEBUG(0,("cups_queue_get: Not enough memory!"));
1198 temp
= queue
+ qcount
;
1199 memset(temp
, 0, sizeof(print_queue_struct
));
1202 * Pull the needed attributes from this job...
1207 job_status
= IPP_JOB_PENDING
;
1213 while (attr
!= NULL
&& attr
->group_tag
== IPP_TAG_JOB
) {
1214 if (attr
->name
== NULL
) {
1219 if (strcmp(attr
->name
, "job-id") == 0 &&
1220 attr
->value_tag
== IPP_TAG_INTEGER
)
1221 job_id
= attr
->values
[0].integer
;
1223 if (strcmp(attr
->name
, "job-k-octets") == 0 &&
1224 attr
->value_tag
== IPP_TAG_INTEGER
)
1225 job_k_octets
= attr
->values
[0].integer
;
1227 if (strcmp(attr
->name
, "job-priority") == 0 &&
1228 attr
->value_tag
== IPP_TAG_INTEGER
)
1229 job_priority
= attr
->values
[0].integer
;
1231 if (strcmp(attr
->name
, "job-state") == 0 &&
1232 attr
->value_tag
== IPP_TAG_ENUM
)
1233 job_status
= (ipp_jstate_t
)(attr
->values
[0].integer
);
1235 if (strcmp(attr
->name
, "time-at-creation") == 0 &&
1236 attr
->value_tag
== IPP_TAG_INTEGER
)
1237 job_time
= attr
->values
[0].integer
;
1239 if (strcmp(attr
->name
, "job-name") == 0 &&
1240 attr
->value_tag
== IPP_TAG_NAME
) {
1241 if (!pull_utf8_talloc(frame
,
1243 attr
->values
[0].string
.text
,
1249 if (strcmp(attr
->name
, "job-originating-user-name") == 0 &&
1250 attr
->value_tag
== IPP_TAG_NAME
) {
1251 if (!pull_utf8_talloc(frame
,
1253 attr
->values
[0].string
.text
,
1263 * See if we have everything needed...
1266 if (user_name
== NULL
|| job_name
== NULL
|| job_id
== 0) {
1274 temp
->size
= job_k_octets
* 1024;
1275 temp
->status
= job_status
== IPP_JOB_PENDING
? LPQ_QUEUED
:
1276 job_status
== IPP_JOB_STOPPED
? LPQ_PAUSED
:
1277 job_status
== IPP_JOB_HELD
? LPQ_PAUSED
:
1279 temp
->priority
= job_priority
;
1280 temp
->time
= job_time
;
1281 strlcpy(temp
->fs_user
, user_name
, sizeof(temp
->fs_user
));
1282 strlcpy(temp
->fs_file
, job_name
, sizeof(temp
->fs_file
));
1290 ippDelete(response
);
1294 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the
1295 * following attributes:
1297 * attributes-charset
1298 * attributes-natural-language
1299 * requested-attributes
1305 request
->request
.op
.operation_id
= IPP_GET_PRINTER_ATTRIBUTES
;
1306 request
->request
.op
.request_id
= 1;
1308 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1309 "attributes-charset", NULL
, "utf-8");
1311 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1312 "attributes-natural-language", NULL
, language
->language
);
1314 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1315 "requested-attributes",
1316 (sizeof(pattrs
) / sizeof(pattrs
[0])),
1319 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1320 "printer-uri", NULL
, uri
);
1323 * Do the request and get back a response...
1326 if ((response
= cupsDoRequest(http
, request
, "/")) == NULL
) {
1327 DEBUG(0,("Unable to get printer status for %s - %s\n", printername
,
1328 ippErrorString(cupsLastError())));
1333 if (response
->request
.status
.status_code
>= IPP_OK_CONFLICT
) {
1334 DEBUG(0,("Unable to get printer status for %s - %s\n", printername
,
1335 ippErrorString(response
->request
.status
.status_code
)));
1341 * Get the current printer status and convert it to the SAMBA values.
1344 if ((attr
= ippFindAttribute(response
, "printer-state", IPP_TAG_ENUM
)) != NULL
) {
1345 if (attr
->values
[0].integer
== IPP_PRINTER_STOPPED
)
1346 status
->status
= LPSTAT_STOPPED
;
1348 status
->status
= LPSTAT_OK
;
1351 if ((attr
= ippFindAttribute(response
, "printer-state-message",
1352 IPP_TAG_TEXT
)) != NULL
) {
1354 if (!pull_utf8_talloc(frame
, &msg
,
1355 attr
->values
[0].string
.text
,
1359 fstrcpy(status
->message
, msg
);
1363 * Return the job queue...
1370 ippDelete(response
);
1373 cupsLangFree(language
);
1384 * 'cups_queue_pause()' - Pause a print queue.
1387 static int cups_queue_pause(int snum
)
1389 TALLOC_CTX
*frame
= talloc_stackframe();
1390 int ret
= 1; /* Return value */
1391 http_t
*http
= NULL
; /* HTTP connection to server */
1392 ipp_t
*request
= NULL
, /* IPP Request */
1393 *response
= NULL
; /* IPP Response */
1394 cups_lang_t
*language
= NULL
; /* Default language */
1395 char *printername
= NULL
;
1396 char *username
= NULL
;
1397 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
1400 DEBUG(5,("cups_queue_pause(%d)\n", snum
));
1403 * Make sure we don't ask for passwords...
1406 cupsSetPasswordCB(cups_passwd_cb
);
1409 * Try to connect to the server...
1412 if ((http
= cups_connect(frame
)) == NULL
) {
1417 * Build an IPP_PAUSE_PRINTER request, which requires the following
1420 * attributes-charset
1421 * attributes-natural-language
1423 * requesting-user-name
1428 request
->request
.op
.operation_id
= IPP_PAUSE_PRINTER
;
1429 request
->request
.op
.request_id
= 1;
1431 language
= cupsLangDefault();
1433 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1434 "attributes-charset", NULL
, "utf-8");
1436 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1437 "attributes-natural-language", NULL
, language
->language
);
1439 if (!push_utf8_talloc(frame
, &printername
, PRINTERNAME(snum
), &size
)) {
1442 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/printers/%s",
1445 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
1447 if (!push_utf8_talloc(frame
, &username
, current_user_info
.unix_name
, &size
)) {
1450 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
1454 * Do the request and get back a response...
1457 if ((response
= cupsDoRequest(http
, request
, "/admin/")) != NULL
) {
1458 if (response
->request
.status
.status_code
>= IPP_OK_CONFLICT
) {
1459 DEBUG(0,("Unable to pause printer %s - %s\n", PRINTERNAME(snum
),
1460 ippErrorString(cupsLastError())));
1465 DEBUG(0,("Unable to pause printer %s - %s\n", PRINTERNAME(snum
),
1466 ippErrorString(cupsLastError())));
1471 ippDelete(response
);
1474 cupsLangFree(language
);
1485 * 'cups_queue_resume()' - Restart a print queue.
1488 static int cups_queue_resume(int snum
)
1490 TALLOC_CTX
*frame
= talloc_stackframe();
1491 int ret
= 1; /* Return value */
1492 http_t
*http
= NULL
; /* HTTP connection to server */
1493 ipp_t
*request
= NULL
, /* IPP Request */
1494 *response
= NULL
; /* IPP Response */
1495 cups_lang_t
*language
= NULL
; /* Default language */
1496 char *printername
= NULL
;
1497 char *username
= NULL
;
1498 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
1501 DEBUG(5,("cups_queue_resume(%d)\n", snum
));
1504 * Make sure we don't ask for passwords...
1507 cupsSetPasswordCB(cups_passwd_cb
);
1510 * Try to connect to the server...
1513 if ((http
= cups_connect(frame
)) == NULL
) {
1518 * Build an IPP_RESUME_PRINTER request, which requires the following
1521 * attributes-charset
1522 * attributes-natural-language
1524 * requesting-user-name
1529 request
->request
.op
.operation_id
= IPP_RESUME_PRINTER
;
1530 request
->request
.op
.request_id
= 1;
1532 language
= cupsLangDefault();
1534 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1535 "attributes-charset", NULL
, "utf-8");
1537 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1538 "attributes-natural-language", NULL
, language
->language
);
1540 if (!push_utf8_talloc(frame
, &printername
, PRINTERNAME(snum
), &size
)) {
1543 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/printers/%s",
1546 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
1548 if (!push_utf8_talloc(frame
, &username
, current_user_info
.unix_name
, &size
)) {
1551 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
1555 * Do the request and get back a response...
1558 if ((response
= cupsDoRequest(http
, request
, "/admin/")) != NULL
) {
1559 if (response
->request
.status
.status_code
>= IPP_OK_CONFLICT
) {
1560 DEBUG(0,("Unable to resume printer %s - %s\n", PRINTERNAME(snum
),
1561 ippErrorString(cupsLastError())));
1566 DEBUG(0,("Unable to resume printer %s - %s\n", PRINTERNAME(snum
),
1567 ippErrorString(cupsLastError())));
1572 ippDelete(response
);
1575 cupsLangFree(language
);
1584 /*******************************************************************
1585 * CUPS printing interface definitions...
1586 ******************************************************************/
1588 struct printif cups_printif
=
1600 bool cups_pull_comment_location(NT_PRINTER_INFO_LEVEL_2
*printer
)
1602 TALLOC_CTX
*frame
= talloc_stackframe();
1603 http_t
*http
= NULL
; /* HTTP connection to server */
1604 ipp_t
*request
= NULL
, /* IPP Request */
1605 *response
= NULL
; /* IPP Response */
1606 ipp_attribute_t
*attr
; /* Current attribute */
1607 cups_lang_t
*language
= NULL
; /* Default language */
1608 char uri
[HTTP_MAX_URI
];
1609 char *server
= NULL
;
1610 char *sharename
= NULL
;
1612 static const char *requested
[] =/* Requested attributes */
1621 DEBUG(5, ("pulling %s location\n", printer
->sharename
));
1624 * Make sure we don't ask for passwords...
1627 cupsSetPasswordCB(cups_passwd_cb
);
1630 * Try to connect to the server...
1633 if ((http
= cups_connect(frame
)) == NULL
) {
1639 request
->request
.op
.operation_id
= IPP_GET_PRINTER_ATTRIBUTES
;
1640 request
->request
.op
.request_id
= 1;
1642 language
= cupsLangDefault();
1644 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1645 "attributes-charset", NULL
, "utf-8");
1647 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1648 "attributes-natural-language", NULL
, language
->language
);
1650 if (lp_cups_server() != NULL
&& strlen(lp_cups_server()) > 0) {
1651 if (!push_utf8_talloc(frame
, &server
, lp_cups_server(), &size
)) {
1655 server
= talloc_strdup(frame
,cupsServer());
1660 if (!push_utf8_talloc(frame
, &sharename
, printer
->sharename
, &size
)) {
1663 slprintf(uri
, sizeof(uri
) - 1, "ipp://%s/printers/%s",
1666 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1667 "printer-uri", NULL
, uri
);
1669 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1670 "requested-attributes",
1671 (sizeof(requested
) / sizeof(requested
[0])),
1675 * Do the request and get back a response...
1678 if ((response
= cupsDoRequest(http
, request
, "/")) == NULL
) {
1679 DEBUG(0,("Unable to get printer attributes - %s\n",
1680 ippErrorString(cupsLastError())));
1684 for (attr
= response
->attrs
; attr
!= NULL
;) {
1686 * Skip leading attributes until we hit a printer...
1689 while (attr
!= NULL
&& attr
->group_tag
!= IPP_TAG_PRINTER
)
1696 * Pull the needed attributes from this printer...
1699 while ( attr
&& (attr
->group_tag
== IPP_TAG_PRINTER
) ) {
1700 if (strcmp(attr
->name
, "printer-name") == 0 &&
1701 attr
->value_tag
== IPP_TAG_NAME
) {
1702 if (!pull_utf8_talloc(frame
,
1704 attr
->values
[0].string
.text
,
1710 /* Grab the comment if we don't have one */
1711 if ( (strcmp(attr
->name
, "printer-info") == 0)
1712 && (attr
->value_tag
== IPP_TAG_TEXT
)
1713 && !strlen(printer
->comment
) )
1715 char *comment
= NULL
;
1716 if (!pull_utf8_talloc(frame
,
1718 attr
->values
[0].string
.text
,
1722 DEBUG(5,("cups_pull_comment_location: Using cups comment: %s\n",
1724 strlcpy(printer
->comment
,
1726 sizeof(printer
->comment
));
1729 /* Grab the location if we don't have one */
1730 if ( (strcmp(attr
->name
, "printer-location") == 0)
1731 && (attr
->value_tag
== IPP_TAG_TEXT
)
1732 && !strlen(printer
->location
) )
1734 char *location
= NULL
;
1735 if (!pull_utf8_talloc(frame
,
1737 attr
->values
[0].string
.text
,
1741 DEBUG(5,("cups_pull_comment_location: Using cups location: %s\n",
1743 strlcpy(printer
->location
,
1745 sizeof(printer
->location
));
1752 * We have everything needed...
1763 ippDelete(response
);
1766 cupsLangFree(language
);
1776 /* this keeps fussy compilers happy */
1777 void print_cups_dummy(void);
1778 void print_cups_dummy(void) {}
1779 #endif /* HAVE_CUPS */