lib: Use close_low_fd in close_low_fds
[Samba.git] / source3 / printing / print_cups.c
blob0ec71ab04a5ff9aadd1b678a3d576ea7b74f2786
1 /*
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.
25 #include "includes.h"
26 #include "printing.h"
27 #include "printing/pcap.h"
28 #include "librpc/gen_ndr/ndr_printcap.h"
30 #ifdef HAVE_CUPS
31 #include <cups/cups.h>
32 #include <cups/language.h>
34 #if (CUPS_VERSION_MAJOR > 1) || (CUPS_VERSION_MINOR > 5)
35 #define HAVE_CUPS_1_6 1
36 #endif
38 #ifndef HAVE_CUPS_1_6
39 #define ippGetGroupTag(attr) attr->group_tag
40 #define ippGetName(attr) attr->name
41 #define ippGetValueTag(attr) attr->value_tag
42 #define ippGetStatusCode(ipp) ipp->request.status.status_code
43 #define ippGetInteger(attr, element) attr->values[element].integer
44 #define ippGetString(attr, element, language) attr->values[element].string.text
46 static ipp_attribute_t *
47 ippFirstAttribute(ipp_t *ipp)
49 if (!ipp)
50 return (NULL);
51 return (ipp->current = ipp->attrs);
54 static ipp_attribute_t *
55 ippNextAttribute(ipp_t *ipp)
57 if (!ipp || !ipp->current)
58 return (NULL);
59 return (ipp->current = ipp->current->next);
62 static int ippSetOperation(ipp_t *ipp, ipp_op_t op)
64 ipp->request.op.operation_id = op;
65 return (1);
68 static int ippSetRequestId(ipp_t *ipp, int request_id)
70 ipp->request.any.request_id = request_id;
71 return (1);
73 #endif
75 static SIG_ATOMIC_T gotalarm;
77 /***************************************************************
78 Signal function to tell us we timed out.
79 ****************************************************************/
81 static void gotalarm_sig(int signum)
83 gotalarm = 1;
86 extern userdom_struct current_user_info;
89 * 'cups_passwd_cb()' - The CUPS password callback...
92 static const char * /* O - Password or NULL */
93 cups_passwd_cb(const char *prompt) /* I - Prompt */
96 * Always return NULL to indicate that no password is available...
99 return (NULL);
102 static http_t *cups_connect(TALLOC_CTX *frame)
104 http_t *http = NULL;
105 char *server = NULL, *p = NULL;
106 int port;
107 int timeout = lp_cups_connection_timeout();
108 size_t size;
110 if (lp_cups_server(talloc_tos()) != NULL && strlen(lp_cups_server(talloc_tos())) > 0) {
111 if (!push_utf8_talloc(frame, &server, lp_cups_server(talloc_tos()), &size)) {
112 return NULL;
114 } else {
115 server = talloc_strdup(frame,cupsServer());
117 if (!server) {
118 return NULL;
121 p = strchr(server, ':');
122 if (p) {
123 port = atoi(p+1);
124 *p = '\0';
125 } else {
126 port = ippPort();
129 DEBUG(10, ("connecting to cups server %s:%d\n",
130 server, port));
132 gotalarm = 0;
134 if (timeout) {
135 CatchSignal(SIGALRM, gotalarm_sig);
136 alarm(timeout);
139 #ifdef HAVE_HTTPCONNECTENCRYPT
140 http = httpConnectEncrypt(server, port, lp_cups_encrypt());
141 #else
142 http = httpConnect(server, port);
143 #endif
146 CatchSignal(SIGALRM, SIG_IGN);
147 alarm(0);
149 if (http == NULL) {
150 DEBUG(0,("Unable to connect to CUPS server %s:%d - %s\n",
151 server, port, strerror(errno)));
154 return http;
157 static bool send_pcap_blob(DATA_BLOB *pcap_blob, int fd)
159 size_t ret;
161 ret = sys_write(fd, &pcap_blob->length, sizeof(pcap_blob->length));
162 if (ret != sizeof(pcap_blob->length)) {
163 return false;
166 ret = sys_write(fd, pcap_blob->data, pcap_blob->length);
167 if (ret != pcap_blob->length) {
168 return false;
171 DEBUG(10, ("successfully sent blob of len %d\n", (int)ret));
172 return true;
175 static bool recv_pcap_blob(TALLOC_CTX *mem_ctx, int fd, DATA_BLOB *pcap_blob)
177 size_t blob_len;
178 size_t ret;
180 ret = sys_read(fd, &blob_len, sizeof(blob_len));
181 if (ret != sizeof(blob_len)) {
182 return false;
185 *pcap_blob = data_blob_talloc_named(mem_ctx, NULL, blob_len,
186 "cups pcap");
187 if (pcap_blob->length != blob_len) {
188 return false;
190 ret = sys_read(fd, pcap_blob->data, blob_len);
191 if (ret != blob_len) {
192 talloc_free(pcap_blob->data);
193 return false;
196 DEBUG(10, ("successfully recvd blob of len %d\n", (int)ret));
197 return true;
200 static bool process_cups_printers_response(TALLOC_CTX *mem_ctx,
201 ipp_t *response,
202 struct pcap_data *pcap_data)
204 ipp_attribute_t *attr;
205 char *name;
206 char *info;
207 char *location = NULL;
208 struct pcap_printer *printer;
209 bool ret_ok = false;
211 for (attr = ippFirstAttribute(response); attr != NULL;) {
213 * Skip leading attributes until we hit a printer...
216 while (attr != NULL && ippGetGroupTag(attr) != IPP_TAG_PRINTER)
217 attr = ippNextAttribute(response);
219 if (attr == NULL)
220 break;
223 * Pull the needed attributes from this printer...
226 name = NULL;
227 info = NULL;
229 while (attr != NULL && ippGetGroupTag(attr) == IPP_TAG_PRINTER) {
230 size_t size;
231 if (strcmp(ippGetName(attr), "printer-name") == 0 &&
232 ippGetValueTag(attr) == IPP_TAG_NAME) {
233 if (!pull_utf8_talloc(mem_ctx,
234 &name,
235 ippGetString(attr, 0, NULL),
236 &size)) {
237 goto err_out;
241 if (strcmp(ippGetName(attr), "printer-info") == 0 &&
242 ippGetValueTag(attr) == IPP_TAG_TEXT) {
243 if (!pull_utf8_talloc(mem_ctx,
244 &info,
245 ippGetString(attr, 0, NULL),
246 &size)) {
247 goto err_out;
251 if (strcmp(ippGetName(attr), "printer-location") == 0 &&
252 ippGetValueTag(attr) == IPP_TAG_TEXT) {
253 if (!pull_utf8_talloc(mem_ctx,
254 &location,
255 ippGetString(attr, 0, NULL),
256 &size)) {
257 goto err_out;
261 attr = ippNextAttribute(response);
265 * See if we have everything needed...
268 if (name == NULL)
269 break;
271 if (pcap_data->count == 0) {
272 printer = talloc_array(mem_ctx, struct pcap_printer, 1);
273 } else {
274 printer = talloc_realloc(mem_ctx, pcap_data->printers,
275 struct pcap_printer,
276 pcap_data->count + 1);
278 if (printer == NULL) {
279 goto err_out;
281 pcap_data->printers = printer;
282 pcap_data->printers[pcap_data->count].name = name;
283 pcap_data->printers[pcap_data->count].info = info;
284 pcap_data->printers[pcap_data->count].location = location;
285 pcap_data->count++;
288 ret_ok = true;
289 err_out:
290 return ret_ok;
294 * request printer list from cups, send result back to up parent via fd.
295 * returns true if the (possibly failed) result was successfuly sent to parent.
297 static bool cups_cache_reload_async(int fd)
299 TALLOC_CTX *frame = talloc_stackframe();
300 struct pcap_data pcap_data;
301 http_t *http = NULL; /* HTTP connection to server */
302 ipp_t *request = NULL, /* IPP Request */
303 *response = NULL; /* IPP Response */
304 cups_lang_t *language = NULL; /* Default language */
305 static const char *requested[] =/* Requested attributes */
307 "printer-name",
308 "printer-info",
309 "printer-location"
311 bool ret = False;
312 enum ndr_err_code ndr_ret;
313 DATA_BLOB pcap_blob;
315 ZERO_STRUCT(pcap_data);
316 pcap_data.status = NT_STATUS_UNSUCCESSFUL;
318 DEBUG(5, ("reloading cups printcap cache\n"));
321 * Make sure we don't ask for passwords...
324 cupsSetPasswordCB(cups_passwd_cb);
326 if ((http = cups_connect(frame)) == NULL) {
327 goto out;
331 * Build a CUPS_GET_PRINTERS request, which requires the following
332 * attributes:
334 * attributes-charset
335 * attributes-natural-language
336 * requested-attributes
339 request = ippNew();
341 ippSetOperation(request, CUPS_GET_PRINTERS);
342 ippSetRequestId(request, 1);
344 language = cupsLangDefault();
346 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
347 "attributes-charset", NULL, "utf-8");
349 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
350 "attributes-natural-language", NULL, language->language);
352 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
353 "requested-attributes",
354 (sizeof(requested) / sizeof(requested[0])),
355 NULL, requested);
357 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
358 DEBUG(0,("Unable to get printer list - %s\n",
359 ippErrorString(cupsLastError())));
360 goto out;
363 ret = process_cups_printers_response(frame, response, &pcap_data);
364 if (!ret) {
365 DEBUG(0,("failed to process cups response\n"));
366 goto out;
369 ippDelete(response);
370 response = NULL;
373 * Build a CUPS_GET_CLASSES request, which requires the following
374 * attributes:
376 * attributes-charset
377 * attributes-natural-language
378 * requested-attributes
381 request = ippNew();
383 ippSetOperation(request, CUPS_GET_CLASSES);
384 ippSetRequestId(request, 1);
386 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
387 "attributes-charset", NULL, "utf-8");
389 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
390 "attributes-natural-language", NULL, language->language);
392 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
393 "requested-attributes",
394 (sizeof(requested) / sizeof(requested[0])),
395 NULL, requested);
397 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
398 DEBUG(0,("Unable to get printer list - %s\n",
399 ippErrorString(cupsLastError())));
400 goto out;
403 ret = process_cups_printers_response(frame, response, &pcap_data);
404 if (!ret) {
405 DEBUG(0,("failed to process cups response\n"));
406 goto out;
409 pcap_data.status = NT_STATUS_OK;
410 out:
411 if (response)
412 ippDelete(response);
414 if (language)
415 cupsLangFree(language);
417 if (http)
418 httpClose(http);
420 ret = false;
421 ndr_ret = ndr_push_struct_blob(&pcap_blob, frame, &pcap_data,
422 (ndr_push_flags_fn_t)ndr_push_pcap_data);
423 if (ndr_ret == NDR_ERR_SUCCESS) {
424 ret = send_pcap_blob(&pcap_blob, fd);
427 TALLOC_FREE(frame);
428 return ret;
431 static struct tevent_fd *cache_fd_event;
433 static bool cups_pcap_load_async(struct tevent_context *ev,
434 struct messaging_context *msg_ctx,
435 int *pfd)
437 int fds[2];
438 pid_t pid;
439 NTSTATUS status;
441 *pfd = -1;
443 if (cache_fd_event) {
444 DEBUG(3,("cups_pcap_load_async: already waiting for "
445 "a refresh event\n" ));
446 return false;
449 DEBUG(5,("cups_pcap_load_async: asynchronously loading cups printers\n"));
451 if (pipe(fds) == -1) {
452 return false;
455 pid = fork();
456 if (pid == (pid_t)-1) {
457 DEBUG(10,("cups_pcap_load_async: fork failed %s\n",
458 strerror(errno) ));
459 close(fds[0]);
460 close(fds[1]);
461 return false;
464 if (pid) {
465 DEBUG(10,("cups_pcap_load_async: child pid = %u\n",
466 (unsigned int)pid ));
467 /* Parent. */
468 close(fds[1]);
469 *pfd = fds[0];
470 return true;
473 /* Child. */
475 close_all_print_db();
477 status = reinit_after_fork(msg_ctx, ev, true);
478 if (!NT_STATUS_IS_OK(status)) {
479 DEBUG(0,("cups_pcap_load_async: reinit_after_fork() failed\n"));
480 smb_panic("cups_pcap_load_async: reinit_after_fork() failed");
483 close(fds[0]);
484 cups_cache_reload_async(fds[1]);
485 close(fds[1]);
486 TALLOC_FREE(msg_ctx);
487 _exit(0);
490 struct cups_async_cb_args {
491 int pipe_fd;
492 struct tevent_context *event_ctx;
493 struct messaging_context *msg_ctx;
494 void (*post_cache_fill_fn)(struct tevent_context *,
495 struct messaging_context *);
498 static void cups_async_callback(struct tevent_context *event_ctx,
499 struct tevent_fd *event,
500 uint16 flags,
501 void *p)
503 TALLOC_CTX *frame = talloc_stackframe();
504 struct cups_async_cb_args *cb_args = (struct cups_async_cb_args *)p;
505 struct pcap_cache *tmp_pcap_cache = NULL;
506 bool ret_ok;
507 struct pcap_data pcap_data;
508 DATA_BLOB pcap_blob;
509 enum ndr_err_code ndr_ret;
510 int i;
512 DEBUG(5,("cups_async_callback: callback received for printer data. "
513 "fd = %d\n", cb_args->pipe_fd));
515 ret_ok = recv_pcap_blob(frame, cb_args->pipe_fd, &pcap_blob);
516 if (!ret_ok) {
517 DEBUG(0,("failed to recv pcap blob\n"));
518 goto err_out;
521 ndr_ret = ndr_pull_struct_blob(&pcap_blob, frame, &pcap_data,
522 (ndr_pull_flags_fn_t)ndr_pull_pcap_data);
523 if (ndr_ret != NDR_ERR_SUCCESS) {
524 goto err_out;
527 if (!NT_STATUS_IS_OK(pcap_data.status)) {
528 DEBUG(0,("failed to retrieve printer list: %s\n",
529 nt_errstr(pcap_data.status)));
530 goto err_out;
533 for (i = 0; i < pcap_data.count; i++) {
534 ret_ok = pcap_cache_add_specific(&tmp_pcap_cache,
535 pcap_data.printers[i].name,
536 pcap_data.printers[i].info,
537 pcap_data.printers[i].location);
538 if (!ret_ok) {
539 DEBUG(0, ("failed to add to tmp pcap cache\n"));
540 goto err_out;
544 /* replace the system-wide pcap cache with a (possibly empty) new one */
545 ret_ok = pcap_cache_replace(tmp_pcap_cache);
546 if (!ret_ok) {
547 DEBUG(0, ("failed to replace pcap cache\n"));
548 } else if (cb_args->post_cache_fill_fn != NULL) {
549 /* Caller requested post cache fill callback */
550 cb_args->post_cache_fill_fn(cb_args->event_ctx,
551 cb_args->msg_ctx);
553 err_out:
554 pcap_cache_destroy_specific(&tmp_pcap_cache);
555 TALLOC_FREE(frame);
556 close(cb_args->pipe_fd);
557 TALLOC_FREE(cb_args);
558 TALLOC_FREE(cache_fd_event);
561 bool cups_cache_reload(struct tevent_context *ev,
562 struct messaging_context *msg_ctx,
563 void (*post_cache_fill_fn)(struct tevent_context *,
564 struct messaging_context *))
566 struct cups_async_cb_args *cb_args;
567 int *p_pipe_fd;
569 cb_args = talloc(NULL, struct cups_async_cb_args);
570 if (cb_args == NULL) {
571 return false;
574 cb_args->post_cache_fill_fn = post_cache_fill_fn;
575 cb_args->event_ctx = ev;
576 cb_args->msg_ctx = msg_ctx;
577 p_pipe_fd = &cb_args->pipe_fd;
578 *p_pipe_fd = -1;
580 /* Set up an async refresh. */
581 if (!cups_pcap_load_async(ev, msg_ctx, p_pipe_fd)) {
582 talloc_free(cb_args);
583 return false;
586 DEBUG(10,("cups_cache_reload: async read on fd %d\n",
587 *p_pipe_fd ));
589 /* Trigger an event when the pipe can be read. */
590 cache_fd_event = tevent_add_fd(ev,
591 NULL, *p_pipe_fd,
592 TEVENT_FD_READ,
593 cups_async_callback,
594 (void *)cb_args);
595 if (!cache_fd_event) {
596 close(*p_pipe_fd);
597 TALLOC_FREE(cb_args);
598 return false;
601 return true;
605 * 'cups_job_delete()' - Delete a job.
608 static int cups_job_delete(const char *sharename, const char *lprm_command, struct printjob *pjob)
610 TALLOC_CTX *frame = talloc_stackframe();
611 int ret = 1; /* Return value */
612 http_t *http = NULL; /* HTTP connection to server */
613 ipp_t *request = NULL, /* IPP Request */
614 *response = NULL; /* IPP Response */
615 cups_lang_t *language = NULL; /* Default language */
616 char *user = NULL;
617 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
618 size_t size;
620 DEBUG(5,("cups_job_delete(%s, %p (%d))\n", sharename, pjob, pjob->sysjob));
623 * Make sure we don't ask for passwords...
626 cupsSetPasswordCB(cups_passwd_cb);
629 * Try to connect to the server...
632 if ((http = cups_connect(frame)) == NULL) {
633 goto out;
637 * Build an IPP_CANCEL_JOB request, which requires the following
638 * attributes:
640 * attributes-charset
641 * attributes-natural-language
642 * job-uri
643 * requesting-user-name
646 request = ippNew();
648 ippSetOperation(request, IPP_CANCEL_JOB);
649 ippSetRequestId(request, 1);
651 language = cupsLangDefault();
653 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
654 "attributes-charset", NULL, "utf-8");
656 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
657 "attributes-natural-language", NULL, language->language);
659 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/jobs/%d", pjob->sysjob);
661 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri);
663 if (!push_utf8_talloc(frame, &user, pjob->user, &size)) {
664 goto out;
667 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
668 NULL, user);
671 * Do the request and get back a response...
674 if ((response = cupsDoRequest(http, request, "/jobs")) != NULL) {
675 if (ippGetStatusCode(response) >= IPP_OK_CONFLICT) {
676 DEBUG(0,("Unable to cancel job %d - %s\n", pjob->sysjob,
677 ippErrorString(cupsLastError())));
678 } else {
679 ret = 0;
681 } else {
682 DEBUG(0,("Unable to cancel job %d - %s\n", pjob->sysjob,
683 ippErrorString(cupsLastError())));
686 out:
687 if (response)
688 ippDelete(response);
690 if (language)
691 cupsLangFree(language);
693 if (http)
694 httpClose(http);
696 TALLOC_FREE(frame);
697 return ret;
702 * 'cups_job_pause()' - Pause a job.
705 static int cups_job_pause(int snum, struct printjob *pjob)
707 TALLOC_CTX *frame = talloc_stackframe();
708 int ret = 1; /* Return value */
709 http_t *http = NULL; /* HTTP connection to server */
710 ipp_t *request = NULL, /* IPP Request */
711 *response = NULL; /* IPP Response */
712 cups_lang_t *language = NULL; /* Default language */
713 char *user = NULL;
714 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
715 size_t size;
717 DEBUG(5,("cups_job_pause(%d, %p (%d))\n", snum, pjob, pjob->sysjob));
720 * Make sure we don't ask for passwords...
723 cupsSetPasswordCB(cups_passwd_cb);
726 * Try to connect to the server...
729 if ((http = cups_connect(frame)) == NULL) {
730 goto out;
734 * Build an IPP_HOLD_JOB request, which requires the following
735 * attributes:
737 * attributes-charset
738 * attributes-natural-language
739 * job-uri
740 * requesting-user-name
743 request = ippNew();
745 ippSetOperation(request, IPP_HOLD_JOB);
746 ippSetRequestId(request, 1);
748 language = cupsLangDefault();
750 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
751 "attributes-charset", NULL, "utf-8");
753 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
754 "attributes-natural-language", NULL, language->language);
756 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/jobs/%d", pjob->sysjob);
758 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri);
760 if (!push_utf8_talloc(frame, &user, pjob->user, &size)) {
761 goto out;
763 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
764 NULL, user);
767 * Do the request and get back a response...
770 if ((response = cupsDoRequest(http, request, "/jobs")) != NULL) {
771 if (ippGetStatusCode(response) >= IPP_OK_CONFLICT) {
772 DEBUG(0,("Unable to hold job %d - %s\n", pjob->sysjob,
773 ippErrorString(cupsLastError())));
774 } else {
775 ret = 0;
777 } else {
778 DEBUG(0,("Unable to hold job %d - %s\n", pjob->sysjob,
779 ippErrorString(cupsLastError())));
782 out:
783 if (response)
784 ippDelete(response);
786 if (language)
787 cupsLangFree(language);
789 if (http)
790 httpClose(http);
792 TALLOC_FREE(frame);
793 return ret;
798 * 'cups_job_resume()' - Resume a paused job.
801 static int cups_job_resume(int snum, struct printjob *pjob)
803 TALLOC_CTX *frame = talloc_stackframe();
804 int ret = 1; /* Return value */
805 http_t *http = NULL; /* HTTP connection to server */
806 ipp_t *request = NULL, /* IPP Request */
807 *response = NULL; /* IPP Response */
808 cups_lang_t *language = NULL; /* Default language */
809 char *user = NULL;
810 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
811 size_t size;
813 DEBUG(5,("cups_job_resume(%d, %p (%d))\n", snum, pjob, pjob->sysjob));
816 * Make sure we don't ask for passwords...
819 cupsSetPasswordCB(cups_passwd_cb);
822 * Try to connect to the server...
825 if ((http = cups_connect(frame)) == NULL) {
826 goto out;
830 * Build an IPP_RELEASE_JOB request, which requires the following
831 * attributes:
833 * attributes-charset
834 * attributes-natural-language
835 * job-uri
836 * requesting-user-name
839 request = ippNew();
841 ippSetOperation(request, IPP_RELEASE_JOB);
842 ippSetRequestId(request, 1);
844 language = cupsLangDefault();
846 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
847 "attributes-charset", NULL, "utf-8");
849 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
850 "attributes-natural-language", NULL, language->language);
852 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/jobs/%d", pjob->sysjob);
854 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri);
856 if (!push_utf8_talloc(frame, &user, pjob->user, &size)) {
857 goto out;
859 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
860 NULL, user);
863 * Do the request and get back a response...
866 if ((response = cupsDoRequest(http, request, "/jobs")) != NULL) {
867 if (ippGetStatusCode(response) >= IPP_OK_CONFLICT) {
868 DEBUG(0,("Unable to release job %d - %s\n", pjob->sysjob,
869 ippErrorString(cupsLastError())));
870 } else {
871 ret = 0;
873 } else {
874 DEBUG(0,("Unable to release job %d - %s\n", pjob->sysjob,
875 ippErrorString(cupsLastError())));
878 out:
879 if (response)
880 ippDelete(response);
882 if (language)
883 cupsLangFree(language);
885 if (http)
886 httpClose(http);
888 TALLOC_FREE(frame);
889 return ret;
894 * 'cups_job_submit()' - Submit a job for printing.
897 static int cups_job_submit(int snum, struct printjob *pjob,
898 enum printing_types printing_type,
899 char *lpq_cmd)
901 TALLOC_CTX *frame = talloc_stackframe();
902 int ret = 1; /* Return value */
903 http_t *http = NULL; /* HTTP connection to server */
904 ipp_t *request = NULL, /* IPP Request */
905 *response = NULL; /* IPP Response */
906 ipp_attribute_t *attr_job_id = NULL; /* IPP Attribute "job-id" */
907 cups_lang_t *language = NULL; /* Default language */
908 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
909 char *new_jobname = NULL;
910 int num_options = 0;
911 cups_option_t *options = NULL;
912 char *printername = NULL;
913 char *user = NULL;
914 char *jobname = NULL;
915 char *cupsoptions = NULL;
916 char *filename = NULL;
917 size_t size;
919 DEBUG(5,("cups_job_submit(%d, %p)\n", snum, pjob));
922 * Make sure we don't ask for passwords...
925 cupsSetPasswordCB(cups_passwd_cb);
928 * Try to connect to the server...
931 if ((http = cups_connect(frame)) == NULL) {
932 goto out;
936 * Build an IPP_PRINT_JOB request, which requires the following
937 * attributes:
939 * attributes-charset
940 * attributes-natural-language
941 * printer-uri
942 * requesting-user-name
943 * [document-data]
946 request = ippNew();
948 ippSetOperation(request, IPP_PRINT_JOB);
949 ippSetRequestId(request, 1);
951 language = cupsLangDefault();
953 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
954 "attributes-charset", NULL, "utf-8");
956 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
957 "attributes-natural-language", NULL, language->language);
959 if (!push_utf8_talloc(frame, &printername,
960 lp_printername(talloc_tos(), snum),
961 &size)) {
962 goto out;
964 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s",
965 printername);
967 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
968 "printer-uri", NULL, uri);
970 if (!push_utf8_talloc(frame, &user, pjob->user, &size)) {
971 goto out;
973 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
974 NULL, user);
976 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
977 "job-originating-host-name", NULL,
978 pjob->clientmachine);
980 if (!push_utf8_talloc(frame, &jobname, pjob->jobname, &size)) {
981 goto out;
983 new_jobname = talloc_asprintf(frame,
984 "%s%.8u %s", PRINT_SPOOL_PREFIX,
985 pjob->jobid, jobname);
986 if (new_jobname == NULL) {
987 goto out;
990 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name", NULL,
991 new_jobname);
994 * add any options defined in smb.conf
997 if (!push_utf8_talloc(frame, &cupsoptions,
998 lp_cups_options(talloc_tos(), snum), &size)) {
999 goto out;
1001 num_options = 0;
1002 options = NULL;
1003 num_options = cupsParseOptions(cupsoptions, num_options, &options);
1005 if ( num_options )
1006 cupsEncodeOptions(request, num_options, options);
1009 * Do the request and get back a response...
1012 slprintf(uri, sizeof(uri) - 1, "/printers/%s", printername);
1014 if (!push_utf8_talloc(frame, &filename, pjob->filename, &size)) {
1015 goto out;
1017 if ((response = cupsDoFileRequest(http, request, uri, pjob->filename)) != NULL) {
1018 if (ippGetStatusCode(response) >= IPP_OK_CONFLICT) {
1019 DEBUG(0,("Unable to print file to %s - %s\n",
1020 lp_printername(talloc_tos(), snum),
1021 ippErrorString(cupsLastError())));
1022 } else {
1023 ret = 0;
1024 attr_job_id = ippFindAttribute(response, "job-id", IPP_TAG_INTEGER);
1025 if(attr_job_id) {
1026 pjob->sysjob = ippGetInteger(attr_job_id, 0);
1027 DEBUG(5,("cups_job_submit: job-id %d\n", pjob->sysjob));
1028 } else {
1029 DEBUG(0,("Missing job-id attribute in IPP response"));
1032 } else {
1033 DEBUG(0,("Unable to print file to `%s' - %s\n",
1034 lp_printername(talloc_tos(), snum),
1035 ippErrorString(cupsLastError())));
1038 if ( ret == 0 )
1039 unlink(pjob->filename);
1040 /* else print_job_end will do it for us */
1042 out:
1043 if (response)
1044 ippDelete(response);
1046 if (language)
1047 cupsLangFree(language);
1049 if (http)
1050 httpClose(http);
1052 TALLOC_FREE(frame);
1054 return ret;
1058 * 'cups_queue_get()' - Get all the jobs in the print queue.
1061 static int cups_queue_get(const char *sharename,
1062 enum printing_types printing_type,
1063 char *lpq_command,
1064 print_queue_struct **q,
1065 print_status_struct *status)
1067 TALLOC_CTX *frame = talloc_stackframe();
1068 char *printername = NULL;
1069 http_t *http = NULL; /* HTTP connection to server */
1070 ipp_t *request = NULL, /* IPP Request */
1071 *response = NULL; /* IPP Response */
1072 ipp_attribute_t *attr = NULL; /* Current attribute */
1073 cups_lang_t *language = NULL; /* Default language */
1074 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
1075 int qcount = 0, /* Number of active queue entries */
1076 qalloc = 0; /* Number of queue entries allocated */
1077 print_queue_struct *queue = NULL, /* Queue entries */
1078 *temp; /* Temporary pointer for queue */
1079 char *user_name = NULL, /* job-originating-user-name attribute */
1080 *job_name = NULL; /* job-name attribute */
1081 int job_id; /* job-id attribute */
1082 int job_k_octets; /* job-k-octets attribute */
1083 time_t job_time; /* time-at-creation attribute */
1084 ipp_jstate_t job_status; /* job-status attribute */
1085 int job_priority; /* job-priority attribute */
1086 size_t size;
1087 static const char *jattrs[] = /* Requested job attributes */
1089 "job-id",
1090 "job-k-octets",
1091 "job-name",
1092 "job-originating-user-name",
1093 "job-priority",
1094 "job-state",
1095 "time-at-creation",
1097 static const char *pattrs[] = /* Requested printer attributes */
1099 "printer-state",
1100 "printer-state-message"
1103 *q = NULL;
1105 /* HACK ALERT!!! The problem with support the 'printer name'
1106 option is that we key the tdb off the sharename. So we will
1107 overload the lpq_command string to pass in the printername
1108 (which is basically what we do for non-cups printers ... using
1109 the lpq_command to get the queue listing). */
1111 if (!push_utf8_talloc(frame, &printername, lpq_command, &size)) {
1112 goto out;
1114 DEBUG(5,("cups_queue_get(%s, %p, %p)\n", lpq_command, q, status));
1117 * Make sure we don't ask for passwords...
1120 cupsSetPasswordCB(cups_passwd_cb);
1123 * Try to connect to the server...
1126 if ((http = cups_connect(frame)) == NULL) {
1127 goto out;
1131 * Generate the printer URI...
1134 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s", printername);
1137 * Build an IPP_GET_JOBS request, which requires the following
1138 * attributes:
1140 * attributes-charset
1141 * attributes-natural-language
1142 * requested-attributes
1143 * printer-uri
1146 request = ippNew();
1148 ippSetOperation(request, IPP_GET_JOBS);
1149 ippSetRequestId(request, 1);
1151 language = cupsLangDefault();
1153 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1154 "attributes-charset", NULL, "utf-8");
1156 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1157 "attributes-natural-language", NULL, language->language);
1159 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1160 "requested-attributes",
1161 (sizeof(jattrs) / sizeof(jattrs[0])),
1162 NULL, jattrs);
1164 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1165 "printer-uri", NULL, uri);
1168 * Do the request and get back a response...
1171 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
1172 DEBUG(0,("Unable to get jobs for %s - %s\n", uri,
1173 ippErrorString(cupsLastError())));
1174 goto out;
1177 if (ippGetStatusCode(response) >= IPP_OK_CONFLICT) {
1178 DEBUG(0,("Unable to get jobs for %s - %s\n", uri,
1179 ippErrorString(ippGetStatusCode(response))));
1180 goto out;
1184 * Process the jobs...
1187 qcount = 0;
1188 qalloc = 0;
1189 queue = NULL;
1191 for (attr = ippFirstAttribute(response); attr != NULL; attr = ippNextAttribute(response)) {
1193 * Skip leading attributes until we hit a job...
1196 while (attr != NULL && ippGetGroupTag(attr) != IPP_TAG_JOB)
1197 attr = ippNextAttribute(response);
1199 if (attr == NULL)
1200 break;
1203 * Allocate memory as needed...
1205 if (qcount >= qalloc) {
1206 qalloc += 16;
1208 queue = SMB_REALLOC_ARRAY(queue, print_queue_struct, qalloc);
1210 if (queue == NULL) {
1211 DEBUG(0,("cups_queue_get: Not enough memory!"));
1212 qcount = 0;
1213 goto out;
1217 temp = queue + qcount;
1218 memset(temp, 0, sizeof(print_queue_struct));
1221 * Pull the needed attributes from this job...
1224 job_id = 0;
1225 job_priority = 50;
1226 job_status = IPP_JOB_PENDING;
1227 job_time = 0;
1228 job_k_octets = 0;
1229 user_name = NULL;
1230 job_name = NULL;
1232 while (attr != NULL && ippGetGroupTag(attr) == IPP_TAG_JOB) {
1233 if (ippGetName(attr) == NULL) {
1234 attr = ippNextAttribute(response);
1235 break;
1238 if (strcmp(ippGetName(attr), "job-id") == 0 &&
1239 ippGetValueTag(attr) == IPP_TAG_INTEGER)
1240 job_id = ippGetInteger(attr, 0);
1242 if (strcmp(ippGetName(attr), "job-k-octets") == 0 &&
1243 ippGetValueTag(attr) == IPP_TAG_INTEGER)
1244 job_k_octets = ippGetInteger(attr, 0);
1246 if (strcmp(ippGetName(attr), "job-priority") == 0 &&
1247 ippGetValueTag(attr) == IPP_TAG_INTEGER)
1248 job_priority = ippGetInteger(attr, 0);
1250 if (strcmp(ippGetName(attr), "job-state") == 0 &&
1251 ippGetValueTag(attr) == IPP_TAG_ENUM)
1252 job_status = (ipp_jstate_t)ippGetInteger(attr, 0);
1254 if (strcmp(ippGetName(attr), "time-at-creation") == 0 &&
1255 ippGetValueTag(attr) == IPP_TAG_INTEGER)
1256 job_time = ippGetInteger(attr, 0);
1258 if (strcmp(ippGetName(attr), "job-name") == 0 &&
1259 ippGetValueTag(attr) == IPP_TAG_NAME) {
1260 if (!pull_utf8_talloc(frame,
1261 &job_name,
1262 ippGetString(attr, 0, NULL),
1263 &size)) {
1264 goto out;
1268 if (strcmp(ippGetName(attr), "job-originating-user-name") == 0 &&
1269 ippGetValueTag(attr) == IPP_TAG_NAME) {
1270 if (!pull_utf8_talloc(frame,
1271 &user_name,
1272 ippGetString(attr, 0, NULL),
1273 &size)) {
1274 goto out;
1278 attr = ippNextAttribute(response);
1282 * See if we have everything needed...
1285 if (user_name == NULL || job_name == NULL || job_id == 0) {
1286 if (attr == NULL)
1287 break;
1288 else
1289 continue;
1292 temp->sysjob = job_id;
1293 temp->size = job_k_octets * 1024;
1294 temp->status = job_status == IPP_JOB_PENDING ? LPQ_QUEUED :
1295 job_status == IPP_JOB_STOPPED ? LPQ_PAUSED :
1296 job_status == IPP_JOB_HELD ? LPQ_PAUSED :
1297 LPQ_PRINTING;
1298 temp->priority = job_priority;
1299 temp->time = job_time;
1300 strlcpy(temp->fs_user, user_name, sizeof(temp->fs_user));
1301 strlcpy(temp->fs_file, job_name, sizeof(temp->fs_file));
1303 qcount ++;
1305 if (attr == NULL)
1306 break;
1309 ippDelete(response);
1310 response = NULL;
1313 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the
1314 * following attributes:
1316 * attributes-charset
1317 * attributes-natural-language
1318 * requested-attributes
1319 * printer-uri
1322 request = ippNew();
1324 ippSetOperation(request, IPP_GET_PRINTER_ATTRIBUTES);
1325 ippSetRequestId(request, 1);
1327 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1328 "attributes-charset", NULL, "utf-8");
1330 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1331 "attributes-natural-language", NULL, language->language);
1333 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1334 "requested-attributes",
1335 (sizeof(pattrs) / sizeof(pattrs[0])),
1336 NULL, pattrs);
1338 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1339 "printer-uri", NULL, uri);
1342 * Do the request and get back a response...
1345 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
1346 DEBUG(0,("Unable to get printer status for %s - %s\n", printername,
1347 ippErrorString(cupsLastError())));
1348 goto out;
1351 if (ippGetStatusCode(response) >= IPP_OK_CONFLICT) {
1352 DEBUG(0,("Unable to get printer status for %s - %s\n", printername,
1353 ippErrorString(ippGetStatusCode(response))));
1354 goto out;
1358 * Get the current printer status and convert it to the SAMBA values.
1361 if ((attr = ippFindAttribute(response, "printer-state", IPP_TAG_ENUM)) != NULL) {
1362 if (ippGetInteger(attr, 0) == IPP_PRINTER_STOPPED)
1363 status->status = LPSTAT_STOPPED;
1364 else
1365 status->status = LPSTAT_OK;
1368 if ((attr = ippFindAttribute(response, "printer-state-message",
1369 IPP_TAG_TEXT)) != NULL) {
1370 char *msg = NULL;
1371 if (!pull_utf8_talloc(frame, &msg,
1372 ippGetString(attr, 0, NULL),
1373 &size)) {
1374 SAFE_FREE(queue);
1375 qcount = 0;
1376 goto out;
1378 fstrcpy(status->message, msg);
1381 out:
1384 * Return the job queue...
1387 *q = queue;
1389 if (response)
1390 ippDelete(response);
1392 if (language)
1393 cupsLangFree(language);
1395 if (http)
1396 httpClose(http);
1398 TALLOC_FREE(frame);
1399 return qcount;
1404 * 'cups_queue_pause()' - Pause a print queue.
1407 static int cups_queue_pause(int snum)
1409 TALLOC_CTX *frame = talloc_stackframe();
1410 int ret = 1; /* Return value */
1411 http_t *http = NULL; /* HTTP connection to server */
1412 ipp_t *request = NULL, /* IPP Request */
1413 *response = NULL; /* IPP Response */
1414 cups_lang_t *language = NULL; /* Default language */
1415 char *printername = NULL;
1416 char *username = NULL;
1417 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
1418 size_t size;
1420 DEBUG(5,("cups_queue_pause(%d)\n", snum));
1423 * Make sure we don't ask for passwords...
1426 cupsSetPasswordCB(cups_passwd_cb);
1429 * Try to connect to the server...
1432 if ((http = cups_connect(frame)) == NULL) {
1433 goto out;
1437 * Build an IPP_PAUSE_PRINTER request, which requires the following
1438 * attributes:
1440 * attributes-charset
1441 * attributes-natural-language
1442 * printer-uri
1443 * requesting-user-name
1446 request = ippNew();
1448 ippSetOperation(request, IPP_PAUSE_PRINTER);
1449 ippSetRequestId(request, 1);
1451 language = cupsLangDefault();
1453 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1454 "attributes-charset", NULL, "utf-8");
1456 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1457 "attributes-natural-language", NULL, language->language);
1459 if (!push_utf8_talloc(frame, &printername,
1460 lp_printername(talloc_tos(), snum), &size)) {
1461 goto out;
1463 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s",
1464 printername);
1466 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
1468 if (!push_utf8_talloc(frame, &username, current_user_info.unix_name, &size)) {
1469 goto out;
1471 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
1472 NULL, username);
1475 * Do the request and get back a response...
1478 if ((response = cupsDoRequest(http, request, "/admin/")) != NULL) {
1479 if (ippGetStatusCode(response) >= IPP_OK_CONFLICT) {
1480 DEBUG(0,("Unable to pause printer %s - %s\n",
1481 lp_printername(talloc_tos(), snum),
1482 ippErrorString(cupsLastError())));
1483 } else {
1484 ret = 0;
1486 } else {
1487 DEBUG(0,("Unable to pause printer %s - %s\n",
1488 lp_printername(talloc_tos(), snum),
1489 ippErrorString(cupsLastError())));
1492 out:
1493 if (response)
1494 ippDelete(response);
1496 if (language)
1497 cupsLangFree(language);
1499 if (http)
1500 httpClose(http);
1502 TALLOC_FREE(frame);
1503 return ret;
1508 * 'cups_queue_resume()' - Restart a print queue.
1511 static int cups_queue_resume(int snum)
1513 TALLOC_CTX *frame = talloc_stackframe();
1514 int ret = 1; /* Return value */
1515 http_t *http = NULL; /* HTTP connection to server */
1516 ipp_t *request = NULL, /* IPP Request */
1517 *response = NULL; /* IPP Response */
1518 cups_lang_t *language = NULL; /* Default language */
1519 char *printername = NULL;
1520 char *username = NULL;
1521 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
1522 size_t size;
1524 DEBUG(5,("cups_queue_resume(%d)\n", snum));
1527 * Make sure we don't ask for passwords...
1530 cupsSetPasswordCB(cups_passwd_cb);
1533 * Try to connect to the server...
1536 if ((http = cups_connect(frame)) == NULL) {
1537 goto out;
1541 * Build an IPP_RESUME_PRINTER request, which requires the following
1542 * attributes:
1544 * attributes-charset
1545 * attributes-natural-language
1546 * printer-uri
1547 * requesting-user-name
1550 request = ippNew();
1552 ippSetOperation(request, IPP_RESUME_PRINTER);
1553 ippSetRequestId(request, 1);
1555 language = cupsLangDefault();
1557 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1558 "attributes-charset", NULL, "utf-8");
1560 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1561 "attributes-natural-language", NULL, language->language);
1563 if (!push_utf8_talloc(frame, &printername, lp_printername(talloc_tos(), snum),
1564 &size)) {
1565 goto out;
1567 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s",
1568 printername);
1570 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
1572 if (!push_utf8_talloc(frame, &username, current_user_info.unix_name, &size)) {
1573 goto out;
1575 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
1576 NULL, username);
1579 * Do the request and get back a response...
1582 if ((response = cupsDoRequest(http, request, "/admin/")) != NULL) {
1583 if (ippGetStatusCode(response) >= IPP_OK_CONFLICT) {
1584 DEBUG(0,("Unable to resume printer %s - %s\n",
1585 lp_printername(talloc_tos(), snum),
1586 ippErrorString(cupsLastError())));
1587 } else {
1588 ret = 0;
1590 } else {
1591 DEBUG(0,("Unable to resume printer %s - %s\n",
1592 lp_printername(talloc_tos(), snum),
1593 ippErrorString(cupsLastError())));
1596 out:
1597 if (response)
1598 ippDelete(response);
1600 if (language)
1601 cupsLangFree(language);
1603 if (http)
1604 httpClose(http);
1606 TALLOC_FREE(frame);
1607 return ret;
1610 /*******************************************************************
1611 * CUPS printing interface definitions...
1612 ******************************************************************/
1614 struct printif cups_printif =
1616 PRINT_CUPS,
1617 cups_queue_get,
1618 cups_queue_pause,
1619 cups_queue_resume,
1620 cups_job_delete,
1621 cups_job_pause,
1622 cups_job_resume,
1623 cups_job_submit,
1626 #else
1627 /* this keeps fussy compilers happy */
1628 void print_cups_dummy(void);
1629 void print_cups_dummy(void) {}
1630 #endif /* HAVE_CUPS */