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(),
394 smbd_event_context(), true)) {
395 DEBUG(0,("cups_pcap_load_async: reinit_after_fork() failed\n"));
396 smb_panic("cups_pcap_load_async: reinit_after_fork() failed");
400 cups_cache_reload_async(fds
[1]);
405 static void cups_async_callback(struct event_context
*event_ctx
,
406 struct fd_event
*event
,
410 TALLOC_CTX
*frame
= talloc_stackframe();
412 struct pcap_cache
*tmp_pcap_cache
= NULL
;
414 DEBUG(5,("cups_async_callback: callback received for printer data. "
418 char *name
= NULL
, *info
= NULL
;
419 size_t namelen
= 0, infolen
= 0;
422 ret
= sys_read(fd
, &namelen
, sizeof(namelen
));
427 if (ret
!= sizeof(namelen
)) {
428 DEBUG(10,("cups_async_callback: namelen read failed %d %s\n",
429 errno
, strerror(errno
)));
433 DEBUG(11,("cups_async_callback: read namelen %u\n",
434 (unsigned int)namelen
));
436 ret
= sys_read(fd
, &infolen
, sizeof(infolen
));
441 if (ret
!= sizeof(infolen
)) {
442 DEBUG(10,("cups_async_callback: infolen read failed %s\n",
447 DEBUG(11,("cups_async_callback: read infolen %u\n",
448 (unsigned int)infolen
));
451 name
= TALLOC_ARRAY(frame
, char, namelen
);
455 ret
= sys_read(fd
, name
, namelen
);
460 if (ret
!= namelen
) {
461 DEBUG(10,("cups_async_callback: name read failed %s\n",
465 DEBUG(11,("cups_async_callback: read name %s\n",
471 info
= TALLOC_ARRAY(frame
, char, infolen
);
475 ret
= sys_read(fd
, info
, infolen
);
480 if (ret
!= infolen
) {
481 DEBUG(10,("cups_async_callback: info read failed %s\n",
485 DEBUG(11,("cups_async_callback: read info %s\n",
491 /* Add to our local pcap cache. */
492 pcap_cache_add_specific(&tmp_pcap_cache
, name
, info
);
498 if (tmp_pcap_cache
) {
499 /* We got a namelist, replace our local cache. */
500 pcap_cache_destroy_specific(&local_pcap_copy
);
501 local_pcap_copy
= tmp_pcap_cache
;
503 /* And the systemwide pcap cache. */
504 pcap_cache_replace(local_pcap_copy
);
506 DEBUG(2,("cups_async_callback: failed to read a new "
511 TALLOC_FREE(cache_fd_event
);
514 bool cups_cache_reload(void)
516 int *p_pipe_fd
= TALLOC_P(NULL
, int);
524 /* Set up an async refresh. */
525 if (!cups_pcap_load_async(p_pipe_fd
)) {
528 if (!local_pcap_copy
) {
529 /* We have no local cache, wait directly for
530 * async refresh to complete.
532 DEBUG(10,("cups_cache_reload: sync read on fd %d\n",
535 cups_async_callback(smbd_event_context(),
539 if (!local_pcap_copy
) {
543 /* Replace the system cache with our
545 pcap_cache_replace(local_pcap_copy
);
547 DEBUG(10,("cups_cache_reload: async read on fd %d\n",
550 /* Trigger an event when the pipe can be read. */
551 cache_fd_event
= event_add_fd(smbd_event_context(),
556 if (!cache_fd_event
) {
558 TALLOC_FREE(p_pipe_fd
);
566 * 'cups_job_delete()' - Delete a job.
569 static int cups_job_delete(const char *sharename
, const char *lprm_command
, struct printjob
*pjob
)
571 TALLOC_CTX
*frame
= talloc_stackframe();
572 int ret
= 1; /* Return value */
573 http_t
*http
= NULL
; /* HTTP connection to server */
574 ipp_t
*request
= NULL
, /* IPP Request */
575 *response
= NULL
; /* IPP Response */
576 cups_lang_t
*language
= NULL
; /* Default language */
578 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
581 DEBUG(5,("cups_job_delete(%s, %p (%d))\n", sharename
, pjob
, pjob
->sysjob
));
584 * Make sure we don't ask for passwords...
587 cupsSetPasswordCB(cups_passwd_cb
);
590 * Try to connect to the server...
593 if ((http
= cups_connect(frame
)) == NULL
) {
598 * Build an IPP_CANCEL_JOB request, which requires the following
602 * attributes-natural-language
604 * requesting-user-name
609 request
->request
.op
.operation_id
= IPP_CANCEL_JOB
;
610 request
->request
.op
.request_id
= 1;
612 language
= cupsLangDefault();
614 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
615 "attributes-charset", NULL
, "utf-8");
617 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
618 "attributes-natural-language", NULL
, language
->language
);
620 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/jobs/%d", pjob
->sysjob
);
622 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
624 if (push_utf8_talloc(frame
, &user
, pjob
->user
) == (size_t)-1) {
628 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
632 * Do the request and get back a response...
635 if ((response
= cupsDoRequest(http
, request
, "/jobs")) != NULL
) {
636 if (response
->request
.status
.status_code
>= IPP_OK_CONFLICT
) {
637 DEBUG(0,("Unable to cancel job %d - %s\n", pjob
->sysjob
,
638 ippErrorString(cupsLastError())));
643 DEBUG(0,("Unable to cancel job %d - %s\n", pjob
->sysjob
,
644 ippErrorString(cupsLastError())));
652 cupsLangFree(language
);
663 * 'cups_job_pause()' - Pause a job.
666 static int cups_job_pause(int snum
, struct printjob
*pjob
)
668 TALLOC_CTX
*frame
= talloc_stackframe();
669 int ret
= 1; /* Return value */
670 http_t
*http
= NULL
; /* HTTP connection to server */
671 ipp_t
*request
= NULL
, /* IPP Request */
672 *response
= NULL
; /* IPP Response */
673 cups_lang_t
*language
= NULL
; /* Default language */
675 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
678 DEBUG(5,("cups_job_pause(%d, %p (%d))\n", snum
, pjob
, pjob
->sysjob
));
681 * Make sure we don't ask for passwords...
684 cupsSetPasswordCB(cups_passwd_cb
);
687 * Try to connect to the server...
690 if ((http
= cups_connect(frame
)) == NULL
) {
695 * Build an IPP_HOLD_JOB request, which requires the following
699 * attributes-natural-language
701 * requesting-user-name
706 request
->request
.op
.operation_id
= IPP_HOLD_JOB
;
707 request
->request
.op
.request_id
= 1;
709 language
= cupsLangDefault();
711 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
712 "attributes-charset", NULL
, "utf-8");
714 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
715 "attributes-natural-language", NULL
, language
->language
);
717 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/jobs/%d", pjob
->sysjob
);
719 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
721 if (push_utf8_talloc(frame
, &user
, pjob
->user
) == (size_t)-1) {
724 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
728 * Do the request and get back a response...
731 if ((response
= cupsDoRequest(http
, request
, "/jobs")) != NULL
) {
732 if (response
->request
.status
.status_code
>= IPP_OK_CONFLICT
) {
733 DEBUG(0,("Unable to hold job %d - %s\n", pjob
->sysjob
,
734 ippErrorString(cupsLastError())));
739 DEBUG(0,("Unable to hold job %d - %s\n", pjob
->sysjob
,
740 ippErrorString(cupsLastError())));
748 cupsLangFree(language
);
759 * 'cups_job_resume()' - Resume a paused job.
762 static int cups_job_resume(int snum
, struct printjob
*pjob
)
764 TALLOC_CTX
*frame
= talloc_stackframe();
765 int ret
= 1; /* Return value */
766 http_t
*http
= NULL
; /* HTTP connection to server */
767 ipp_t
*request
= NULL
, /* IPP Request */
768 *response
= NULL
; /* IPP Response */
769 cups_lang_t
*language
= NULL
; /* Default language */
771 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
774 DEBUG(5,("cups_job_resume(%d, %p (%d))\n", snum
, pjob
, pjob
->sysjob
));
777 * Make sure we don't ask for passwords...
780 cupsSetPasswordCB(cups_passwd_cb
);
783 * Try to connect to the server...
786 if ((http
= cups_connect(frame
)) == NULL
) {
791 * Build an IPP_RELEASE_JOB request, which requires the following
795 * attributes-natural-language
797 * requesting-user-name
802 request
->request
.op
.operation_id
= IPP_RELEASE_JOB
;
803 request
->request
.op
.request_id
= 1;
805 language
= cupsLangDefault();
807 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
808 "attributes-charset", NULL
, "utf-8");
810 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
811 "attributes-natural-language", NULL
, language
->language
);
813 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/jobs/%d", pjob
->sysjob
);
815 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
817 if (push_utf8_talloc(frame
, &user
, pjob
->user
) == (size_t)-1) {
820 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
824 * Do the request and get back a response...
827 if ((response
= cupsDoRequest(http
, request
, "/jobs")) != NULL
) {
828 if (response
->request
.status
.status_code
>= IPP_OK_CONFLICT
) {
829 DEBUG(0,("Unable to release job %d - %s\n", pjob
->sysjob
,
830 ippErrorString(cupsLastError())));
835 DEBUG(0,("Unable to release job %d - %s\n", pjob
->sysjob
,
836 ippErrorString(cupsLastError())));
844 cupsLangFree(language
);
855 * 'cups_job_submit()' - Submit a job for printing.
858 static int cups_job_submit(int snum
, struct printjob
*pjob
)
860 TALLOC_CTX
*frame
= talloc_stackframe();
861 int ret
= 1; /* Return value */
862 http_t
*http
= NULL
; /* HTTP connection to server */
863 ipp_t
*request
= NULL
, /* IPP Request */
864 *response
= NULL
; /* IPP Response */
865 cups_lang_t
*language
= NULL
; /* Default language */
866 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
867 const char *clientname
= NULL
; /* hostname of client for job-originating-host attribute */
868 char *new_jobname
= NULL
;
870 cups_option_t
*options
= NULL
;
871 char *printername
= NULL
;
873 char *jobname
= NULL
;
874 char *cupsoptions
= NULL
;
875 char *filename
= NULL
;
876 char addr
[INET6_ADDRSTRLEN
];
878 DEBUG(5,("cups_job_submit(%d, %p (%d))\n", snum
, pjob
, pjob
->sysjob
));
881 * Make sure we don't ask for passwords...
884 cupsSetPasswordCB(cups_passwd_cb
);
887 * Try to connect to the server...
890 if ((http
= cups_connect(frame
)) == NULL
) {
895 * Build an IPP_PRINT_JOB request, which requires the following
899 * attributes-natural-language
901 * requesting-user-name
907 request
->request
.op
.operation_id
= IPP_PRINT_JOB
;
908 request
->request
.op
.request_id
= 1;
910 language
= cupsLangDefault();
912 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
913 "attributes-charset", NULL
, "utf-8");
915 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
916 "attributes-natural-language", NULL
, language
->language
);
918 if (push_utf8_talloc(frame
, &printername
, PRINTERNAME(snum
)) == (size_t)-1) {
921 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/printers/%s",
924 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
925 "printer-uri", NULL
, uri
);
927 if (push_utf8_talloc(frame
, &user
, pjob
->user
) == (size_t)-1) {
930 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
933 clientname
= client_name(get_client_fd());
934 if (strcmp(clientname
, "UNKNOWN") == 0) {
935 clientname
= client_addr(get_client_fd(),addr
,sizeof(addr
));
938 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
939 "job-originating-host-name", NULL
,
942 if (push_utf8_talloc(frame
, &jobname
, pjob
->jobname
) == (size_t)-1) {
945 new_jobname
= talloc_asprintf(frame
,
946 "%s%.8u %s", PRINT_SPOOL_PREFIX
,
947 (unsigned int)pjob
->smbjob
,
949 if (new_jobname
== NULL
) {
953 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name", NULL
,
957 * add any options defined in smb.conf
960 if (push_utf8_talloc(frame
, &cupsoptions
, lp_cups_options(snum
)) == (size_t)-1) {
965 num_options
= cupsParseOptions(cupsoptions
, num_options
, &options
);
968 cupsEncodeOptions(request
, num_options
, options
);
971 * Do the request and get back a response...
974 slprintf(uri
, sizeof(uri
) - 1, "/printers/%s", printername
);
976 if (push_utf8_talloc(frame
, &filename
, pjob
->filename
) == (size_t)-1) {
979 if ((response
= cupsDoFileRequest(http
, request
, uri
, pjob
->filename
)) != NULL
) {
980 if (response
->request
.status
.status_code
>= IPP_OK_CONFLICT
) {
981 DEBUG(0,("Unable to print file to %s - %s\n", PRINTERNAME(snum
),
982 ippErrorString(cupsLastError())));
987 DEBUG(0,("Unable to print file to `%s' - %s\n", PRINTERNAME(snum
),
988 ippErrorString(cupsLastError())));
992 unlink(pjob
->filename
);
993 /* else print_job_end will do it for us */
1000 cupsLangFree(language
);
1011 * 'cups_queue_get()' - Get all the jobs in the print queue.
1014 static int cups_queue_get(const char *sharename
,
1015 enum printing_types printing_type
,
1017 print_queue_struct
**q
,
1018 print_status_struct
*status
)
1020 TALLOC_CTX
*frame
= talloc_stackframe();
1021 char *printername
= NULL
;
1022 http_t
*http
= NULL
; /* HTTP connection to server */
1023 ipp_t
*request
= NULL
, /* IPP Request */
1024 *response
= NULL
; /* IPP Response */
1025 ipp_attribute_t
*attr
= NULL
; /* Current attribute */
1026 cups_lang_t
*language
= NULL
; /* Default language */
1027 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
1028 int qcount
= 0, /* Number of active queue entries */
1029 qalloc
= 0; /* Number of queue entries allocated */
1030 print_queue_struct
*queue
= NULL
, /* Queue entries */
1031 *temp
; /* Temporary pointer for queue */
1032 char *user_name
= NULL
, /* job-originating-user-name attribute */
1033 *job_name
= NULL
; /* job-name attribute */
1034 int job_id
; /* job-id attribute */
1035 int job_k_octets
; /* job-k-octets attribute */
1036 time_t job_time
; /* time-at-creation attribute */
1037 ipp_jstate_t job_status
; /* job-status attribute */
1038 int job_priority
; /* job-priority attribute */
1039 static const char *jattrs
[] = /* Requested job attributes */
1044 "job-originating-user-name",
1049 static const char *pattrs
[] = /* Requested printer attributes */
1052 "printer-state-message"
1057 /* HACK ALERT!!! The problem with support the 'printer name'
1058 option is that we key the tdb off the sharename. So we will
1059 overload the lpq_command string to pass in the printername
1060 (which is basically what we do for non-cups printers ... using
1061 the lpq_command to get the queue listing). */
1063 if (push_utf8_talloc(frame
, &printername
, lpq_command
) == (size_t)-1) {
1066 DEBUG(5,("cups_queue_get(%s, %p, %p)\n", lpq_command
, q
, status
));
1069 * Make sure we don't ask for passwords...
1072 cupsSetPasswordCB(cups_passwd_cb
);
1075 * Try to connect to the server...
1078 if ((http
= cups_connect(frame
)) == NULL
) {
1083 * Generate the printer URI...
1086 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/printers/%s", printername
);
1089 * Build an IPP_GET_JOBS request, which requires the following
1092 * attributes-charset
1093 * attributes-natural-language
1094 * requested-attributes
1100 request
->request
.op
.operation_id
= IPP_GET_JOBS
;
1101 request
->request
.op
.request_id
= 1;
1103 language
= cupsLangDefault();
1105 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1106 "attributes-charset", NULL
, "utf-8");
1108 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1109 "attributes-natural-language", NULL
, language
->language
);
1111 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1112 "requested-attributes",
1113 (sizeof(jattrs
) / sizeof(jattrs
[0])),
1116 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1117 "printer-uri", NULL
, uri
);
1120 * Do the request and get back a response...
1123 if ((response
= cupsDoRequest(http
, request
, "/")) == NULL
) {
1124 DEBUG(0,("Unable to get jobs for %s - %s\n", uri
,
1125 ippErrorString(cupsLastError())));
1129 if (response
->request
.status
.status_code
>= IPP_OK_CONFLICT
) {
1130 DEBUG(0,("Unable to get jobs for %s - %s\n", uri
,
1131 ippErrorString(response
->request
.status
.status_code
)));
1136 * Process the jobs...
1143 for (attr
= response
->attrs
; attr
!= NULL
; attr
= attr
->next
) {
1145 * Skip leading attributes until we hit a job...
1148 while (attr
!= NULL
&& attr
->group_tag
!= IPP_TAG_JOB
)
1155 * Allocate memory as needed...
1157 if (qcount
>= qalloc
) {
1160 queue
= SMB_REALLOC_ARRAY(queue
, print_queue_struct
, qalloc
);
1162 if (queue
== NULL
) {
1163 DEBUG(0,("cups_queue_get: Not enough memory!"));
1169 temp
= queue
+ qcount
;
1170 memset(temp
, 0, sizeof(print_queue_struct
));
1173 * Pull the needed attributes from this job...
1178 job_status
= IPP_JOB_PENDING
;
1184 while (attr
!= NULL
&& attr
->group_tag
== IPP_TAG_JOB
) {
1185 if (attr
->name
== NULL
) {
1190 if (strcmp(attr
->name
, "job-id") == 0 &&
1191 attr
->value_tag
== IPP_TAG_INTEGER
)
1192 job_id
= attr
->values
[0].integer
;
1194 if (strcmp(attr
->name
, "job-k-octets") == 0 &&
1195 attr
->value_tag
== IPP_TAG_INTEGER
)
1196 job_k_octets
= attr
->values
[0].integer
;
1198 if (strcmp(attr
->name
, "job-priority") == 0 &&
1199 attr
->value_tag
== IPP_TAG_INTEGER
)
1200 job_priority
= attr
->values
[0].integer
;
1202 if (strcmp(attr
->name
, "job-state") == 0 &&
1203 attr
->value_tag
== IPP_TAG_ENUM
)
1204 job_status
= (ipp_jstate_t
)(attr
->values
[0].integer
);
1206 if (strcmp(attr
->name
, "time-at-creation") == 0 &&
1207 attr
->value_tag
== IPP_TAG_INTEGER
)
1208 job_time
= attr
->values
[0].integer
;
1210 if (strcmp(attr
->name
, "job-name") == 0 &&
1211 attr
->value_tag
== IPP_TAG_NAME
) {
1212 pull_utf8_talloc(frame
,
1214 attr
->values
[0].string
.text
);
1217 if (strcmp(attr
->name
, "job-originating-user-name") == 0 &&
1218 attr
->value_tag
== IPP_TAG_NAME
) {
1219 pull_utf8_talloc(frame
,
1221 attr
->values
[0].string
.text
);
1228 * See if we have everything needed...
1231 if (user_name
== NULL
|| job_name
== NULL
|| job_id
== 0) {
1239 temp
->size
= job_k_octets
* 1024;
1240 temp
->status
= job_status
== IPP_JOB_PENDING
? LPQ_QUEUED
:
1241 job_status
== IPP_JOB_STOPPED
? LPQ_PAUSED
:
1242 job_status
== IPP_JOB_HELD
? LPQ_PAUSED
:
1244 temp
->priority
= job_priority
;
1245 temp
->time
= job_time
;
1246 strlcpy(temp
->fs_user
, user_name
, sizeof(temp
->fs_user
));
1247 strlcpy(temp
->fs_file
, job_name
, sizeof(temp
->fs_file
));
1255 ippDelete(response
);
1259 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the
1260 * following attributes:
1262 * attributes-charset
1263 * attributes-natural-language
1264 * requested-attributes
1270 request
->request
.op
.operation_id
= IPP_GET_PRINTER_ATTRIBUTES
;
1271 request
->request
.op
.request_id
= 1;
1273 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1274 "attributes-charset", NULL
, "utf-8");
1276 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1277 "attributes-natural-language", NULL
, language
->language
);
1279 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1280 "requested-attributes",
1281 (sizeof(pattrs
) / sizeof(pattrs
[0])),
1284 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1285 "printer-uri", NULL
, uri
);
1288 * Do the request and get back a response...
1291 if ((response
= cupsDoRequest(http
, request
, "/")) == NULL
) {
1292 DEBUG(0,("Unable to get printer status for %s - %s\n", printername
,
1293 ippErrorString(cupsLastError())));
1298 if (response
->request
.status
.status_code
>= IPP_OK_CONFLICT
) {
1299 DEBUG(0,("Unable to get printer status for %s - %s\n", printername
,
1300 ippErrorString(response
->request
.status
.status_code
)));
1306 * Get the current printer status and convert it to the SAMBA values.
1309 if ((attr
= ippFindAttribute(response
, "printer-state", IPP_TAG_ENUM
)) != NULL
) {
1310 if (attr
->values
[0].integer
== IPP_PRINTER_STOPPED
)
1311 status
->status
= LPSTAT_STOPPED
;
1313 status
->status
= LPSTAT_OK
;
1316 if ((attr
= ippFindAttribute(response
, "printer-state-message",
1317 IPP_TAG_TEXT
)) != NULL
) {
1319 pull_utf8_talloc(frame
, &msg
, attr
->values
[0].string
.text
);
1320 fstrcpy(status
->message
, msg
);
1324 * Return the job queue...
1331 ippDelete(response
);
1334 cupsLangFree(language
);
1345 * 'cups_queue_pause()' - Pause a print queue.
1348 static int cups_queue_pause(int snum
)
1350 TALLOC_CTX
*frame
= talloc_stackframe();
1351 int ret
= 1; /* Return value */
1352 http_t
*http
= NULL
; /* HTTP connection to server */
1353 ipp_t
*request
= NULL
, /* IPP Request */
1354 *response
= NULL
; /* IPP Response */
1355 cups_lang_t
*language
= NULL
; /* Default language */
1356 char *printername
= NULL
;
1357 char *username
= NULL
;
1358 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
1361 DEBUG(5,("cups_queue_pause(%d)\n", snum
));
1364 * Make sure we don't ask for passwords...
1367 cupsSetPasswordCB(cups_passwd_cb
);
1370 * Try to connect to the server...
1373 if ((http
= cups_connect(frame
)) == NULL
) {
1378 * Build an IPP_PAUSE_PRINTER request, which requires the following
1381 * attributes-charset
1382 * attributes-natural-language
1384 * requesting-user-name
1389 request
->request
.op
.operation_id
= IPP_PAUSE_PRINTER
;
1390 request
->request
.op
.request_id
= 1;
1392 language
= cupsLangDefault();
1394 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1395 "attributes-charset", NULL
, "utf-8");
1397 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1398 "attributes-natural-language", NULL
, language
->language
);
1400 if (push_utf8_talloc(frame
, &printername
, PRINTERNAME(snum
)) == (size_t)-1) {
1403 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/printers/%s",
1406 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
1408 if (push_utf8_talloc(frame
, &username
, current_user_info
.unix_name
) == (size_t)-1) {
1411 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
1415 * Do the request and get back a response...
1418 if ((response
= cupsDoRequest(http
, request
, "/admin/")) != NULL
) {
1419 if (response
->request
.status
.status_code
>= IPP_OK_CONFLICT
) {
1420 DEBUG(0,("Unable to pause printer %s - %s\n", PRINTERNAME(snum
),
1421 ippErrorString(cupsLastError())));
1426 DEBUG(0,("Unable to pause printer %s - %s\n", PRINTERNAME(snum
),
1427 ippErrorString(cupsLastError())));
1432 ippDelete(response
);
1435 cupsLangFree(language
);
1446 * 'cups_queue_resume()' - Restart a print queue.
1449 static int cups_queue_resume(int snum
)
1451 TALLOC_CTX
*frame
= talloc_stackframe();
1452 int ret
= 1; /* Return value */
1453 http_t
*http
= NULL
; /* HTTP connection to server */
1454 ipp_t
*request
= NULL
, /* IPP Request */
1455 *response
= NULL
; /* IPP Response */
1456 cups_lang_t
*language
= NULL
; /* Default language */
1457 char *printername
= NULL
;
1458 char *username
= NULL
;
1459 char uri
[HTTP_MAX_URI
]; /* printer-uri attribute */
1462 DEBUG(5,("cups_queue_resume(%d)\n", snum
));
1465 * Make sure we don't ask for passwords...
1468 cupsSetPasswordCB(cups_passwd_cb
);
1471 * Try to connect to the server...
1474 if ((http
= cups_connect(frame
)) == NULL
) {
1479 * Build an IPP_RESUME_PRINTER request, which requires the following
1482 * attributes-charset
1483 * attributes-natural-language
1485 * requesting-user-name
1490 request
->request
.op
.operation_id
= IPP_RESUME_PRINTER
;
1491 request
->request
.op
.request_id
= 1;
1493 language
= cupsLangDefault();
1495 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1496 "attributes-charset", NULL
, "utf-8");
1498 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1499 "attributes-natural-language", NULL
, language
->language
);
1501 if (push_utf8_talloc(frame
, &printername
, PRINTERNAME(snum
)) == (size_t)-1) {
1504 slprintf(uri
, sizeof(uri
) - 1, "ipp://localhost/printers/%s",
1507 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
1509 if (push_utf8_talloc(frame
, &username
, current_user_info
.unix_name
) == (size_t)-1) {
1512 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
1516 * Do the request and get back a response...
1519 if ((response
= cupsDoRequest(http
, request
, "/admin/")) != NULL
) {
1520 if (response
->request
.status
.status_code
>= IPP_OK_CONFLICT
) {
1521 DEBUG(0,("Unable to resume printer %s - %s\n", PRINTERNAME(snum
),
1522 ippErrorString(cupsLastError())));
1527 DEBUG(0,("Unable to resume printer %s - %s\n", PRINTERNAME(snum
),
1528 ippErrorString(cupsLastError())));
1533 ippDelete(response
);
1536 cupsLangFree(language
);
1545 /*******************************************************************
1546 * CUPS printing interface definitions...
1547 ******************************************************************/
1549 struct printif cups_printif
=
1561 bool cups_pull_comment_location(NT_PRINTER_INFO_LEVEL_2
*printer
)
1563 TALLOC_CTX
*frame
= talloc_stackframe();
1564 http_t
*http
= NULL
; /* HTTP connection to server */
1565 ipp_t
*request
= NULL
, /* IPP Request */
1566 *response
= NULL
; /* IPP Response */
1567 ipp_attribute_t
*attr
; /* Current attribute */
1568 cups_lang_t
*language
= NULL
; /* Default language */
1569 char uri
[HTTP_MAX_URI
];
1570 char *server
= NULL
;
1571 char *sharename
= NULL
;
1573 static const char *requested
[] =/* Requested attributes */
1581 DEBUG(5, ("pulling %s location\n", printer
->sharename
));
1584 * Make sure we don't ask for passwords...
1587 cupsSetPasswordCB(cups_passwd_cb
);
1590 * Try to connect to the server...
1593 if ((http
= cups_connect(frame
)) == NULL
) {
1599 request
->request
.op
.operation_id
= IPP_GET_PRINTER_ATTRIBUTES
;
1600 request
->request
.op
.request_id
= 1;
1602 language
= cupsLangDefault();
1604 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1605 "attributes-charset", NULL
, "utf-8");
1607 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1608 "attributes-natural-language", NULL
, language
->language
);
1610 if (lp_cups_server() != NULL
&& strlen(lp_cups_server()) > 0) {
1611 if (push_utf8_talloc(frame
, &server
, lp_cups_server()) == (size_t)-1) {
1615 server
= talloc_strdup(frame
,cupsServer());
1620 if (push_utf8_talloc(frame
, &sharename
, printer
->sharename
) == (size_t)-1) {
1623 slprintf(uri
, sizeof(uri
) - 1, "ipp://%s/printers/%s",
1626 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1627 "printer-uri", NULL
, uri
);
1629 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1630 "requested-attributes",
1631 (sizeof(requested
) / sizeof(requested
[0])),
1635 * Do the request and get back a response...
1638 if ((response
= cupsDoRequest(http
, request
, "/")) == NULL
) {
1639 DEBUG(0,("Unable to get printer attributes - %s\n",
1640 ippErrorString(cupsLastError())));
1644 for (attr
= response
->attrs
; attr
!= NULL
;) {
1646 * Skip leading attributes until we hit a printer...
1649 while (attr
!= NULL
&& attr
->group_tag
!= IPP_TAG_PRINTER
)
1656 * Pull the needed attributes from this printer...
1659 while ( attr
&& (attr
->group_tag
== IPP_TAG_PRINTER
) ) {
1660 if (strcmp(attr
->name
, "printer-name") == 0 &&
1661 attr
->value_tag
== IPP_TAG_NAME
) {
1662 pull_utf8_talloc(frame
,
1664 attr
->values
[0].string
.text
);
1667 /* Grab the comment if we don't have one */
1668 if ( (strcmp(attr
->name
, "printer-info") == 0)
1669 && (attr
->value_tag
== IPP_TAG_TEXT
)
1670 && !strlen(printer
->comment
) )
1672 char *comment
= NULL
;
1673 pull_utf8_talloc(frame
,
1675 attr
->values
[0].string
.text
);
1676 DEBUG(5,("cups_pull_comment_location: Using cups comment: %s\n",
1678 strlcpy(printer
->comment
,
1680 sizeof(printer
->comment
));
1683 /* Grab the location if we don't have one */
1684 if ( (strcmp(attr
->name
, "printer-location") == 0)
1685 && (attr
->value_tag
== IPP_TAG_TEXT
)
1686 && !strlen(printer
->location
) )
1688 char *location
= NULL
;
1689 pull_utf8_talloc(frame
,
1691 attr
->values
[0].string
.text
);
1692 DEBUG(5,("cups_pull_comment_location: Using cups location: %s\n",
1694 strlcpy(printer
->location
,
1696 sizeof(printer
->location
));
1703 * We have everything needed...
1714 ippDelete(response
);
1721 cupsLangFree(language
);
1731 /* this keeps fussy compilers happy */
1732 void print_cups_dummy(void);
1733 void print_cups_dummy(void) {}
1734 #endif /* HAVE_CUPS */