loadparm: rename lp[cfg]_print_ok to lp[cfg]_printable for consistency with docs
[Samba.git] / source3 / printing / print_cups.c
blob6c1e9ce23c80671c405ed4a04e41e19ba19eab34
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 _exit(0);
489 struct cups_async_cb_args {
490 int pipe_fd;
491 struct tevent_context *event_ctx;
492 struct messaging_context *msg_ctx;
493 void (*post_cache_fill_fn)(struct tevent_context *,
494 struct messaging_context *);
497 static void cups_async_callback(struct tevent_context *event_ctx,
498 struct tevent_fd *event,
499 uint16 flags,
500 void *p)
502 TALLOC_CTX *frame = talloc_stackframe();
503 struct cups_async_cb_args *cb_args = (struct cups_async_cb_args *)p;
504 struct pcap_cache *tmp_pcap_cache = NULL;
505 bool ret_ok;
506 struct pcap_data pcap_data;
507 DATA_BLOB pcap_blob;
508 enum ndr_err_code ndr_ret;
509 int i;
511 DEBUG(5,("cups_async_callback: callback received for printer data. "
512 "fd = %d\n", cb_args->pipe_fd));
514 ret_ok = recv_pcap_blob(frame, cb_args->pipe_fd, &pcap_blob);
515 if (!ret_ok) {
516 DEBUG(0,("failed to recv pcap blob\n"));
517 goto err_out;
520 ndr_ret = ndr_pull_struct_blob(&pcap_blob, frame, &pcap_data,
521 (ndr_pull_flags_fn_t)ndr_pull_pcap_data);
522 if (ndr_ret != NDR_ERR_SUCCESS) {
523 goto err_out;
526 if (!NT_STATUS_IS_OK(pcap_data.status)) {
527 DEBUG(0,("failed to retrieve printer list: %s\n",
528 nt_errstr(pcap_data.status)));
529 goto err_out;
532 for (i = 0; i < pcap_data.count; i++) {
533 ret_ok = pcap_cache_add_specific(&tmp_pcap_cache,
534 pcap_data.printers[i].name,
535 pcap_data.printers[i].info,
536 pcap_data.printers[i].location);
537 if (!ret_ok) {
538 DEBUG(0, ("failed to add to tmp pcap cache\n"));
539 goto err_out;
543 /* replace the system-wide pcap cache with a (possibly empty) new one */
544 ret_ok = pcap_cache_replace(tmp_pcap_cache);
545 if (!ret_ok) {
546 DEBUG(0, ("failed to replace pcap cache\n"));
547 } else if (cb_args->post_cache_fill_fn != NULL) {
548 /* Caller requested post cache fill callback */
549 cb_args->post_cache_fill_fn(cb_args->event_ctx,
550 cb_args->msg_ctx);
552 err_out:
553 pcap_cache_destroy_specific(&tmp_pcap_cache);
554 TALLOC_FREE(frame);
555 close(cb_args->pipe_fd);
556 TALLOC_FREE(cb_args);
557 TALLOC_FREE(cache_fd_event);
560 bool cups_cache_reload(struct tevent_context *ev,
561 struct messaging_context *msg_ctx,
562 void (*post_cache_fill_fn)(struct tevent_context *,
563 struct messaging_context *))
565 struct cups_async_cb_args *cb_args;
566 int *p_pipe_fd;
568 cb_args = talloc(NULL, struct cups_async_cb_args);
569 if (cb_args == NULL) {
570 return false;
573 cb_args->post_cache_fill_fn = post_cache_fill_fn;
574 cb_args->event_ctx = ev;
575 cb_args->msg_ctx = msg_ctx;
576 p_pipe_fd = &cb_args->pipe_fd;
577 *p_pipe_fd = -1;
579 /* Set up an async refresh. */
580 if (!cups_pcap_load_async(ev, msg_ctx, p_pipe_fd)) {
581 talloc_free(cb_args);
582 return false;
585 DEBUG(10,("cups_cache_reload: async read on fd %d\n",
586 *p_pipe_fd ));
588 /* Trigger an event when the pipe can be read. */
589 cache_fd_event = tevent_add_fd(ev,
590 NULL, *p_pipe_fd,
591 TEVENT_FD_READ,
592 cups_async_callback,
593 (void *)cb_args);
594 if (!cache_fd_event) {
595 close(*p_pipe_fd);
596 TALLOC_FREE(cb_args);
597 return false;
600 return true;
604 * 'cups_job_delete()' - Delete a job.
607 static int cups_job_delete(const char *sharename, const char *lprm_command, struct printjob *pjob)
609 TALLOC_CTX *frame = talloc_stackframe();
610 int ret = 1; /* Return value */
611 http_t *http = NULL; /* HTTP connection to server */
612 ipp_t *request = NULL, /* IPP Request */
613 *response = NULL; /* IPP Response */
614 cups_lang_t *language = NULL; /* Default language */
615 char *user = NULL;
616 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
617 size_t size;
619 DEBUG(5,("cups_job_delete(%s, %p (%d))\n", sharename, pjob, pjob->sysjob));
622 * Make sure we don't ask for passwords...
625 cupsSetPasswordCB(cups_passwd_cb);
628 * Try to connect to the server...
631 if ((http = cups_connect(frame)) == NULL) {
632 goto out;
636 * Build an IPP_CANCEL_JOB request, which requires the following
637 * attributes:
639 * attributes-charset
640 * attributes-natural-language
641 * job-uri
642 * requesting-user-name
645 request = ippNew();
647 ippSetOperation(request, IPP_CANCEL_JOB);
648 ippSetRequestId(request, 1);
650 language = cupsLangDefault();
652 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
653 "attributes-charset", NULL, "utf-8");
655 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
656 "attributes-natural-language", NULL, language->language);
658 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/jobs/%d", pjob->sysjob);
660 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri);
662 if (!push_utf8_talloc(frame, &user, pjob->user, &size)) {
663 goto out;
666 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
667 NULL, user);
670 * Do the request and get back a response...
673 if ((response = cupsDoRequest(http, request, "/jobs")) != NULL) {
674 if (ippGetStatusCode(response) >= IPP_OK_CONFLICT) {
675 DEBUG(0,("Unable to cancel job %d - %s\n", pjob->sysjob,
676 ippErrorString(cupsLastError())));
677 } else {
678 ret = 0;
680 } else {
681 DEBUG(0,("Unable to cancel job %d - %s\n", pjob->sysjob,
682 ippErrorString(cupsLastError())));
685 out:
686 if (response)
687 ippDelete(response);
689 if (language)
690 cupsLangFree(language);
692 if (http)
693 httpClose(http);
695 TALLOC_FREE(frame);
696 return ret;
701 * 'cups_job_pause()' - Pause a job.
704 static int cups_job_pause(int snum, struct printjob *pjob)
706 TALLOC_CTX *frame = talloc_stackframe();
707 int ret = 1; /* Return value */
708 http_t *http = NULL; /* HTTP connection to server */
709 ipp_t *request = NULL, /* IPP Request */
710 *response = NULL; /* IPP Response */
711 cups_lang_t *language = NULL; /* Default language */
712 char *user = NULL;
713 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
714 size_t size;
716 DEBUG(5,("cups_job_pause(%d, %p (%d))\n", snum, pjob, pjob->sysjob));
719 * Make sure we don't ask for passwords...
722 cupsSetPasswordCB(cups_passwd_cb);
725 * Try to connect to the server...
728 if ((http = cups_connect(frame)) == NULL) {
729 goto out;
733 * Build an IPP_HOLD_JOB request, which requires the following
734 * attributes:
736 * attributes-charset
737 * attributes-natural-language
738 * job-uri
739 * requesting-user-name
742 request = ippNew();
744 ippSetOperation(request, IPP_HOLD_JOB);
745 ippSetRequestId(request, 1);
747 language = cupsLangDefault();
749 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
750 "attributes-charset", NULL, "utf-8");
752 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
753 "attributes-natural-language", NULL, language->language);
755 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/jobs/%d", pjob->sysjob);
757 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri);
759 if (!push_utf8_talloc(frame, &user, pjob->user, &size)) {
760 goto out;
762 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
763 NULL, user);
766 * Do the request and get back a response...
769 if ((response = cupsDoRequest(http, request, "/jobs")) != NULL) {
770 if (ippGetStatusCode(response) >= IPP_OK_CONFLICT) {
771 DEBUG(0,("Unable to hold job %d - %s\n", pjob->sysjob,
772 ippErrorString(cupsLastError())));
773 } else {
774 ret = 0;
776 } else {
777 DEBUG(0,("Unable to hold job %d - %s\n", pjob->sysjob,
778 ippErrorString(cupsLastError())));
781 out:
782 if (response)
783 ippDelete(response);
785 if (language)
786 cupsLangFree(language);
788 if (http)
789 httpClose(http);
791 TALLOC_FREE(frame);
792 return ret;
797 * 'cups_job_resume()' - Resume a paused job.
800 static int cups_job_resume(int snum, struct printjob *pjob)
802 TALLOC_CTX *frame = talloc_stackframe();
803 int ret = 1; /* Return value */
804 http_t *http = NULL; /* HTTP connection to server */
805 ipp_t *request = NULL, /* IPP Request */
806 *response = NULL; /* IPP Response */
807 cups_lang_t *language = NULL; /* Default language */
808 char *user = NULL;
809 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
810 size_t size;
812 DEBUG(5,("cups_job_resume(%d, %p (%d))\n", snum, pjob, pjob->sysjob));
815 * Make sure we don't ask for passwords...
818 cupsSetPasswordCB(cups_passwd_cb);
821 * Try to connect to the server...
824 if ((http = cups_connect(frame)) == NULL) {
825 goto out;
829 * Build an IPP_RELEASE_JOB request, which requires the following
830 * attributes:
832 * attributes-charset
833 * attributes-natural-language
834 * job-uri
835 * requesting-user-name
838 request = ippNew();
840 ippSetOperation(request, IPP_RELEASE_JOB);
841 ippSetRequestId(request, 1);
843 language = cupsLangDefault();
845 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
846 "attributes-charset", NULL, "utf-8");
848 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
849 "attributes-natural-language", NULL, language->language);
851 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/jobs/%d", pjob->sysjob);
853 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri);
855 if (!push_utf8_talloc(frame, &user, pjob->user, &size)) {
856 goto out;
858 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
859 NULL, user);
862 * Do the request and get back a response...
865 if ((response = cupsDoRequest(http, request, "/jobs")) != NULL) {
866 if (ippGetStatusCode(response) >= IPP_OK_CONFLICT) {
867 DEBUG(0,("Unable to release job %d - %s\n", pjob->sysjob,
868 ippErrorString(cupsLastError())));
869 } else {
870 ret = 0;
872 } else {
873 DEBUG(0,("Unable to release job %d - %s\n", pjob->sysjob,
874 ippErrorString(cupsLastError())));
877 out:
878 if (response)
879 ippDelete(response);
881 if (language)
882 cupsLangFree(language);
884 if (http)
885 httpClose(http);
887 TALLOC_FREE(frame);
888 return ret;
893 * 'cups_job_submit()' - Submit a job for printing.
896 static int cups_job_submit(int snum, struct printjob *pjob,
897 enum printing_types printing_type,
898 char *lpq_cmd)
900 TALLOC_CTX *frame = talloc_stackframe();
901 int ret = 1; /* Return value */
902 http_t *http = NULL; /* HTTP connection to server */
903 ipp_t *request = NULL, /* IPP Request */
904 *response = NULL; /* IPP Response */
905 ipp_attribute_t *attr_job_id = NULL; /* IPP Attribute "job-id" */
906 cups_lang_t *language = NULL; /* Default language */
907 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
908 char *new_jobname = NULL;
909 int num_options = 0;
910 cups_option_t *options = NULL;
911 char *printername = NULL;
912 char *user = NULL;
913 char *jobname = NULL;
914 char *cupsoptions = NULL;
915 char *filename = NULL;
916 size_t size;
918 DEBUG(5,("cups_job_submit(%d, %p)\n", snum, pjob));
921 * Make sure we don't ask for passwords...
924 cupsSetPasswordCB(cups_passwd_cb);
927 * Try to connect to the server...
930 if ((http = cups_connect(frame)) == NULL) {
931 goto out;
935 * Build an IPP_PRINT_JOB request, which requires the following
936 * attributes:
938 * attributes-charset
939 * attributes-natural-language
940 * printer-uri
941 * requesting-user-name
942 * [document-data]
945 request = ippNew();
947 ippSetOperation(request, IPP_PRINT_JOB);
948 ippSetRequestId(request, 1);
950 language = cupsLangDefault();
952 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
953 "attributes-charset", NULL, "utf-8");
955 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
956 "attributes-natural-language", NULL, language->language);
958 if (!push_utf8_talloc(frame, &printername,
959 lp_printername(talloc_tos(), snum),
960 &size)) {
961 goto out;
963 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s",
964 printername);
966 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
967 "printer-uri", NULL, uri);
969 if (!push_utf8_talloc(frame, &user, pjob->user, &size)) {
970 goto out;
972 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
973 NULL, user);
975 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
976 "job-originating-host-name", NULL,
977 pjob->clientmachine);
979 if (!push_utf8_talloc(frame, &jobname, pjob->jobname, &size)) {
980 goto out;
982 new_jobname = talloc_asprintf(frame,
983 "%s%.8u %s", PRINT_SPOOL_PREFIX,
984 pjob->jobid, jobname);
985 if (new_jobname == NULL) {
986 goto out;
989 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name", NULL,
990 new_jobname);
993 * add any options defined in smb.conf
996 if (!push_utf8_talloc(frame, &cupsoptions,
997 lp_cups_options(talloc_tos(), snum), &size)) {
998 goto out;
1000 num_options = 0;
1001 options = NULL;
1002 num_options = cupsParseOptions(cupsoptions, num_options, &options);
1004 if ( num_options )
1005 cupsEncodeOptions(request, num_options, options);
1008 * Do the request and get back a response...
1011 slprintf(uri, sizeof(uri) - 1, "/printers/%s", printername);
1013 if (!push_utf8_talloc(frame, &filename, pjob->filename, &size)) {
1014 goto out;
1016 if ((response = cupsDoFileRequest(http, request, uri, pjob->filename)) != NULL) {
1017 if (ippGetStatusCode(response) >= IPP_OK_CONFLICT) {
1018 DEBUG(0,("Unable to print file to %s - %s\n",
1019 lp_printername(talloc_tos(), snum),
1020 ippErrorString(cupsLastError())));
1021 } else {
1022 ret = 0;
1023 attr_job_id = ippFindAttribute(response, "job-id", IPP_TAG_INTEGER);
1024 if(attr_job_id) {
1025 pjob->sysjob = ippGetInteger(attr_job_id, 0);
1026 DEBUG(5,("cups_job_submit: job-id %d\n", pjob->sysjob));
1027 } else {
1028 DEBUG(0,("Missing job-id attribute in IPP response"));
1031 } else {
1032 DEBUG(0,("Unable to print file to `%s' - %s\n",
1033 lp_printername(talloc_tos(), snum),
1034 ippErrorString(cupsLastError())));
1037 if ( ret == 0 )
1038 unlink(pjob->filename);
1039 /* else print_job_end will do it for us */
1041 out:
1042 if (response)
1043 ippDelete(response);
1045 if (language)
1046 cupsLangFree(language);
1048 if (http)
1049 httpClose(http);
1051 TALLOC_FREE(frame);
1053 return ret;
1057 * 'cups_queue_get()' - Get all the jobs in the print queue.
1060 static int cups_queue_get(const char *sharename,
1061 enum printing_types printing_type,
1062 char *lpq_command,
1063 print_queue_struct **q,
1064 print_status_struct *status)
1066 TALLOC_CTX *frame = talloc_stackframe();
1067 char *printername = NULL;
1068 http_t *http = NULL; /* HTTP connection to server */
1069 ipp_t *request = NULL, /* IPP Request */
1070 *response = NULL; /* IPP Response */
1071 ipp_attribute_t *attr = NULL; /* Current attribute */
1072 cups_lang_t *language = NULL; /* Default language */
1073 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
1074 int qcount = 0, /* Number of active queue entries */
1075 qalloc = 0; /* Number of queue entries allocated */
1076 print_queue_struct *queue = NULL, /* Queue entries */
1077 *temp; /* Temporary pointer for queue */
1078 char *user_name = NULL, /* job-originating-user-name attribute */
1079 *job_name = NULL; /* job-name attribute */
1080 int job_id; /* job-id attribute */
1081 int job_k_octets; /* job-k-octets attribute */
1082 time_t job_time; /* time-at-creation attribute */
1083 ipp_jstate_t job_status; /* job-status attribute */
1084 int job_priority; /* job-priority attribute */
1085 size_t size;
1086 static const char *jattrs[] = /* Requested job attributes */
1088 "job-id",
1089 "job-k-octets",
1090 "job-name",
1091 "job-originating-user-name",
1092 "job-priority",
1093 "job-state",
1094 "time-at-creation",
1096 static const char *pattrs[] = /* Requested printer attributes */
1098 "printer-state",
1099 "printer-state-message"
1102 *q = NULL;
1104 /* HACK ALERT!!! The problem with support the 'printer name'
1105 option is that we key the tdb off the sharename. So we will
1106 overload the lpq_command string to pass in the printername
1107 (which is basically what we do for non-cups printers ... using
1108 the lpq_command to get the queue listing). */
1110 if (!push_utf8_talloc(frame, &printername, lpq_command, &size)) {
1111 goto out;
1113 DEBUG(5,("cups_queue_get(%s, %p, %p)\n", lpq_command, q, status));
1116 * Make sure we don't ask for passwords...
1119 cupsSetPasswordCB(cups_passwd_cb);
1122 * Try to connect to the server...
1125 if ((http = cups_connect(frame)) == NULL) {
1126 goto out;
1130 * Generate the printer URI...
1133 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s", printername);
1136 * Build an IPP_GET_JOBS request, which requires the following
1137 * attributes:
1139 * attributes-charset
1140 * attributes-natural-language
1141 * requested-attributes
1142 * printer-uri
1145 request = ippNew();
1147 ippSetOperation(request, IPP_GET_JOBS);
1148 ippSetRequestId(request, 1);
1150 language = cupsLangDefault();
1152 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1153 "attributes-charset", NULL, "utf-8");
1155 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1156 "attributes-natural-language", NULL, language->language);
1158 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1159 "requested-attributes",
1160 (sizeof(jattrs) / sizeof(jattrs[0])),
1161 NULL, jattrs);
1163 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1164 "printer-uri", NULL, uri);
1167 * Do the request and get back a response...
1170 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
1171 DEBUG(0,("Unable to get jobs for %s - %s\n", uri,
1172 ippErrorString(cupsLastError())));
1173 goto out;
1176 if (ippGetStatusCode(response) >= IPP_OK_CONFLICT) {
1177 DEBUG(0,("Unable to get jobs for %s - %s\n", uri,
1178 ippErrorString(ippGetStatusCode(response))));
1179 goto out;
1183 * Process the jobs...
1186 qcount = 0;
1187 qalloc = 0;
1188 queue = NULL;
1190 for (attr = ippFirstAttribute(response); attr != NULL; attr = ippNextAttribute(response)) {
1192 * Skip leading attributes until we hit a job...
1195 while (attr != NULL && ippGetGroupTag(attr) != IPP_TAG_JOB)
1196 attr = ippNextAttribute(response);
1198 if (attr == NULL)
1199 break;
1202 * Allocate memory as needed...
1204 if (qcount >= qalloc) {
1205 qalloc += 16;
1207 queue = SMB_REALLOC_ARRAY(queue, print_queue_struct, qalloc);
1209 if (queue == NULL) {
1210 DEBUG(0,("cups_queue_get: Not enough memory!"));
1211 qcount = 0;
1212 goto out;
1216 temp = queue + qcount;
1217 memset(temp, 0, sizeof(print_queue_struct));
1220 * Pull the needed attributes from this job...
1223 job_id = 0;
1224 job_priority = 50;
1225 job_status = IPP_JOB_PENDING;
1226 job_time = 0;
1227 job_k_octets = 0;
1228 user_name = NULL;
1229 job_name = NULL;
1231 while (attr != NULL && ippGetGroupTag(attr) == IPP_TAG_JOB) {
1232 if (ippGetName(attr) == NULL) {
1233 attr = ippNextAttribute(response);
1234 break;
1237 if (strcmp(ippGetName(attr), "job-id") == 0 &&
1238 ippGetValueTag(attr) == IPP_TAG_INTEGER)
1239 job_id = ippGetInteger(attr, 0);
1241 if (strcmp(ippGetName(attr), "job-k-octets") == 0 &&
1242 ippGetValueTag(attr) == IPP_TAG_INTEGER)
1243 job_k_octets = ippGetInteger(attr, 0);
1245 if (strcmp(ippGetName(attr), "job-priority") == 0 &&
1246 ippGetValueTag(attr) == IPP_TAG_INTEGER)
1247 job_priority = ippGetInteger(attr, 0);
1249 if (strcmp(ippGetName(attr), "job-state") == 0 &&
1250 ippGetValueTag(attr) == IPP_TAG_ENUM)
1251 job_status = (ipp_jstate_t)ippGetInteger(attr, 0);
1253 if (strcmp(ippGetName(attr), "time-at-creation") == 0 &&
1254 ippGetValueTag(attr) == IPP_TAG_INTEGER)
1255 job_time = ippGetInteger(attr, 0);
1257 if (strcmp(ippGetName(attr), "job-name") == 0 &&
1258 ippGetValueTag(attr) == IPP_TAG_NAME) {
1259 if (!pull_utf8_talloc(frame,
1260 &job_name,
1261 ippGetString(attr, 0, NULL),
1262 &size)) {
1263 goto out;
1267 if (strcmp(ippGetName(attr), "job-originating-user-name") == 0 &&
1268 ippGetValueTag(attr) == IPP_TAG_NAME) {
1269 if (!pull_utf8_talloc(frame,
1270 &user_name,
1271 ippGetString(attr, 0, NULL),
1272 &size)) {
1273 goto out;
1277 attr = ippNextAttribute(response);
1281 * See if we have everything needed...
1284 if (user_name == NULL || job_name == NULL || job_id == 0) {
1285 if (attr == NULL)
1286 break;
1287 else
1288 continue;
1291 temp->sysjob = job_id;
1292 temp->size = job_k_octets * 1024;
1293 temp->status = job_status == IPP_JOB_PENDING ? LPQ_QUEUED :
1294 job_status == IPP_JOB_STOPPED ? LPQ_PAUSED :
1295 job_status == IPP_JOB_HELD ? LPQ_PAUSED :
1296 LPQ_PRINTING;
1297 temp->priority = job_priority;
1298 temp->time = job_time;
1299 strlcpy(temp->fs_user, user_name, sizeof(temp->fs_user));
1300 strlcpy(temp->fs_file, job_name, sizeof(temp->fs_file));
1302 qcount ++;
1304 if (attr == NULL)
1305 break;
1308 ippDelete(response);
1309 response = NULL;
1312 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the
1313 * following attributes:
1315 * attributes-charset
1316 * attributes-natural-language
1317 * requested-attributes
1318 * printer-uri
1321 request = ippNew();
1323 ippSetOperation(request, IPP_GET_PRINTER_ATTRIBUTES);
1324 ippSetRequestId(request, 1);
1326 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1327 "attributes-charset", NULL, "utf-8");
1329 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1330 "attributes-natural-language", NULL, language->language);
1332 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1333 "requested-attributes",
1334 (sizeof(pattrs) / sizeof(pattrs[0])),
1335 NULL, pattrs);
1337 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1338 "printer-uri", NULL, uri);
1341 * Do the request and get back a response...
1344 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
1345 DEBUG(0,("Unable to get printer status for %s - %s\n", printername,
1346 ippErrorString(cupsLastError())));
1347 goto out;
1350 if (ippGetStatusCode(response) >= IPP_OK_CONFLICT) {
1351 DEBUG(0,("Unable to get printer status for %s - %s\n", printername,
1352 ippErrorString(ippGetStatusCode(response))));
1353 goto out;
1357 * Get the current printer status and convert it to the SAMBA values.
1360 if ((attr = ippFindAttribute(response, "printer-state", IPP_TAG_ENUM)) != NULL) {
1361 if (ippGetInteger(attr, 0) == IPP_PRINTER_STOPPED)
1362 status->status = LPSTAT_STOPPED;
1363 else
1364 status->status = LPSTAT_OK;
1367 if ((attr = ippFindAttribute(response, "printer-state-message",
1368 IPP_TAG_TEXT)) != NULL) {
1369 char *msg = NULL;
1370 if (!pull_utf8_talloc(frame, &msg,
1371 ippGetString(attr, 0, NULL),
1372 &size)) {
1373 SAFE_FREE(queue);
1374 qcount = 0;
1375 goto out;
1377 fstrcpy(status->message, msg);
1380 out:
1383 * Return the job queue...
1386 *q = queue;
1388 if (response)
1389 ippDelete(response);
1391 if (language)
1392 cupsLangFree(language);
1394 if (http)
1395 httpClose(http);
1397 TALLOC_FREE(frame);
1398 return qcount;
1403 * 'cups_queue_pause()' - Pause a print queue.
1406 static int cups_queue_pause(int snum)
1408 TALLOC_CTX *frame = talloc_stackframe();
1409 int ret = 1; /* Return value */
1410 http_t *http = NULL; /* HTTP connection to server */
1411 ipp_t *request = NULL, /* IPP Request */
1412 *response = NULL; /* IPP Response */
1413 cups_lang_t *language = NULL; /* Default language */
1414 char *printername = NULL;
1415 char *username = NULL;
1416 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
1417 size_t size;
1419 DEBUG(5,("cups_queue_pause(%d)\n", snum));
1422 * Make sure we don't ask for passwords...
1425 cupsSetPasswordCB(cups_passwd_cb);
1428 * Try to connect to the server...
1431 if ((http = cups_connect(frame)) == NULL) {
1432 goto out;
1436 * Build an IPP_PAUSE_PRINTER request, which requires the following
1437 * attributes:
1439 * attributes-charset
1440 * attributes-natural-language
1441 * printer-uri
1442 * requesting-user-name
1445 request = ippNew();
1447 ippSetOperation(request, IPP_PAUSE_PRINTER);
1448 ippSetRequestId(request, 1);
1450 language = cupsLangDefault();
1452 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1453 "attributes-charset", NULL, "utf-8");
1455 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1456 "attributes-natural-language", NULL, language->language);
1458 if (!push_utf8_talloc(frame, &printername,
1459 lp_printername(talloc_tos(), snum), &size)) {
1460 goto out;
1462 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s",
1463 printername);
1465 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
1467 if (!push_utf8_talloc(frame, &username, current_user_info.unix_name, &size)) {
1468 goto out;
1470 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
1471 NULL, username);
1474 * Do the request and get back a response...
1477 if ((response = cupsDoRequest(http, request, "/admin/")) != NULL) {
1478 if (ippGetStatusCode(response) >= IPP_OK_CONFLICT) {
1479 DEBUG(0,("Unable to pause printer %s - %s\n",
1480 lp_printername(talloc_tos(), snum),
1481 ippErrorString(cupsLastError())));
1482 } else {
1483 ret = 0;
1485 } else {
1486 DEBUG(0,("Unable to pause printer %s - %s\n",
1487 lp_printername(talloc_tos(), snum),
1488 ippErrorString(cupsLastError())));
1491 out:
1492 if (response)
1493 ippDelete(response);
1495 if (language)
1496 cupsLangFree(language);
1498 if (http)
1499 httpClose(http);
1501 TALLOC_FREE(frame);
1502 return ret;
1507 * 'cups_queue_resume()' - Restart a print queue.
1510 static int cups_queue_resume(int snum)
1512 TALLOC_CTX *frame = talloc_stackframe();
1513 int ret = 1; /* Return value */
1514 http_t *http = NULL; /* HTTP connection to server */
1515 ipp_t *request = NULL, /* IPP Request */
1516 *response = NULL; /* IPP Response */
1517 cups_lang_t *language = NULL; /* Default language */
1518 char *printername = NULL;
1519 char *username = NULL;
1520 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
1521 size_t size;
1523 DEBUG(5,("cups_queue_resume(%d)\n", snum));
1526 * Make sure we don't ask for passwords...
1529 cupsSetPasswordCB(cups_passwd_cb);
1532 * Try to connect to the server...
1535 if ((http = cups_connect(frame)) == NULL) {
1536 goto out;
1540 * Build an IPP_RESUME_PRINTER request, which requires the following
1541 * attributes:
1543 * attributes-charset
1544 * attributes-natural-language
1545 * printer-uri
1546 * requesting-user-name
1549 request = ippNew();
1551 ippSetOperation(request, IPP_RESUME_PRINTER);
1552 ippSetRequestId(request, 1);
1554 language = cupsLangDefault();
1556 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1557 "attributes-charset", NULL, "utf-8");
1559 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1560 "attributes-natural-language", NULL, language->language);
1562 if (!push_utf8_talloc(frame, &printername, lp_printername(talloc_tos(), snum),
1563 &size)) {
1564 goto out;
1566 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s",
1567 printername);
1569 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
1571 if (!push_utf8_talloc(frame, &username, current_user_info.unix_name, &size)) {
1572 goto out;
1574 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
1575 NULL, username);
1578 * Do the request and get back a response...
1581 if ((response = cupsDoRequest(http, request, "/admin/")) != NULL) {
1582 if (ippGetStatusCode(response) >= IPP_OK_CONFLICT) {
1583 DEBUG(0,("Unable to resume printer %s - %s\n",
1584 lp_printername(talloc_tos(), snum),
1585 ippErrorString(cupsLastError())));
1586 } else {
1587 ret = 0;
1589 } else {
1590 DEBUG(0,("Unable to resume printer %s - %s\n",
1591 lp_printername(talloc_tos(), snum),
1592 ippErrorString(cupsLastError())));
1595 out:
1596 if (response)
1597 ippDelete(response);
1599 if (language)
1600 cupsLangFree(language);
1602 if (http)
1603 httpClose(http);
1605 TALLOC_FREE(frame);
1606 return ret;
1609 /*******************************************************************
1610 * CUPS printing interface definitions...
1611 ******************************************************************/
1613 struct printif cups_printif =
1615 PRINT_CUPS,
1616 cups_queue_get,
1617 cups_queue_pause,
1618 cups_queue_resume,
1619 cups_job_delete,
1620 cups_job_pause,
1621 cups_job_resume,
1622 cups_job_submit,
1625 #else
1626 /* this keeps fussy compilers happy */
1627 void print_cups_dummy(void);
1628 void print_cups_dummy(void) {}
1629 #endif /* HAVE_CUPS */