WHATSNEW: Start release notes for Samba 3.6.16.
[Samba.git] / source3 / printing / print_cups.c
blob67af9655150873aeaec26ae156e4927ba8e7dcb3
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() != NULL && strlen(lp_cups_server()) > 0) {
111 if (!push_utf8_talloc(frame, &server, lp_cups_server(), &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 fd_event *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 = sys_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, procid_self(), 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 event_context *event_ctx;
492 struct messaging_context *msg_ctx;
493 void (*post_cache_fill_fn)(struct event_context *,
494 struct messaging_context *);
497 static void cups_async_callback(struct event_context *event_ctx,
498 struct fd_event *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_P(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 = event_add_fd(ev,
590 NULL, *p_pipe_fd,
591 EVENT_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, lp_printername(snum),
959 &size)) {
960 goto out;
962 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s",
963 printername);
965 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
966 "printer-uri", NULL, uri);
968 if (!push_utf8_talloc(frame, &user, pjob->user, &size)) {
969 goto out;
971 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
972 NULL, user);
974 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
975 "job-originating-host-name", NULL,
976 pjob->clientmachine);
978 if (!push_utf8_talloc(frame, &jobname, pjob->jobname, &size)) {
979 goto out;
981 new_jobname = talloc_asprintf(frame,
982 "%s%.8u %s", PRINT_SPOOL_PREFIX,
983 pjob->jobid, jobname);
984 if (new_jobname == NULL) {
985 goto out;
988 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name", NULL,
989 new_jobname);
992 * add any options defined in smb.conf
995 if (!push_utf8_talloc(frame, &cupsoptions, lp_cups_options(snum), &size)) {
996 goto out;
998 num_options = 0;
999 options = NULL;
1000 num_options = cupsParseOptions(cupsoptions, num_options, &options);
1002 if ( num_options )
1003 cupsEncodeOptions(request, num_options, options);
1006 * Do the request and get back a response...
1009 slprintf(uri, sizeof(uri) - 1, "/printers/%s", printername);
1011 if (!push_utf8_talloc(frame, &filename, pjob->filename, &size)) {
1012 goto out;
1014 if ((response = cupsDoFileRequest(http, request, uri, pjob->filename)) != NULL) {
1015 if (ippGetStatusCode(response) >= IPP_OK_CONFLICT) {
1016 DEBUG(0,("Unable to print file to %s - %s\n",
1017 lp_printername(snum),
1018 ippErrorString(cupsLastError())));
1019 } else {
1020 ret = 0;
1021 attr_job_id = ippFindAttribute(response, "job-id", IPP_TAG_INTEGER);
1022 if(attr_job_id) {
1023 pjob->sysjob = ippGetInteger(attr_job_id, 0);
1024 DEBUG(5,("cups_job_submit: job-id %d\n", pjob->sysjob));
1025 } else {
1026 DEBUG(0,("Missing job-id attribute in IPP response"));
1029 } else {
1030 DEBUG(0,("Unable to print file to `%s' - %s\n",
1031 lp_printername(snum),
1032 ippErrorString(cupsLastError())));
1035 if ( ret == 0 )
1036 unlink(pjob->filename);
1037 /* else print_job_end will do it for us */
1039 out:
1040 if (response)
1041 ippDelete(response);
1043 if (language)
1044 cupsLangFree(language);
1046 if (http)
1047 httpClose(http);
1049 TALLOC_FREE(frame);
1051 return ret;
1055 * 'cups_queue_get()' - Get all the jobs in the print queue.
1058 static int cups_queue_get(const char *sharename,
1059 enum printing_types printing_type,
1060 char *lpq_command,
1061 print_queue_struct **q,
1062 print_status_struct *status)
1064 TALLOC_CTX *frame = talloc_stackframe();
1065 char *printername = NULL;
1066 http_t *http = NULL; /* HTTP connection to server */
1067 ipp_t *request = NULL, /* IPP Request */
1068 *response = NULL; /* IPP Response */
1069 ipp_attribute_t *attr = NULL; /* Current attribute */
1070 cups_lang_t *language = NULL; /* Default language */
1071 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
1072 int qcount = 0, /* Number of active queue entries */
1073 qalloc = 0; /* Number of queue entries allocated */
1074 print_queue_struct *queue = NULL, /* Queue entries */
1075 *temp; /* Temporary pointer for queue */
1076 char *user_name = NULL, /* job-originating-user-name attribute */
1077 *job_name = NULL; /* job-name attribute */
1078 int job_id; /* job-id attribute */
1079 int job_k_octets; /* job-k-octets attribute */
1080 time_t job_time; /* time-at-creation attribute */
1081 ipp_jstate_t job_status; /* job-status attribute */
1082 int job_priority; /* job-priority attribute */
1083 size_t size;
1084 static const char *jattrs[] = /* Requested job attributes */
1086 "job-id",
1087 "job-k-octets",
1088 "job-name",
1089 "job-originating-user-name",
1090 "job-priority",
1091 "job-state",
1092 "time-at-creation",
1094 static const char *pattrs[] = /* Requested printer attributes */
1096 "printer-state",
1097 "printer-state-message"
1100 *q = NULL;
1102 /* HACK ALERT!!! The problem with support the 'printer name'
1103 option is that we key the tdb off the sharename. So we will
1104 overload the lpq_command string to pass in the printername
1105 (which is basically what we do for non-cups printers ... using
1106 the lpq_command to get the queue listing). */
1108 if (!push_utf8_talloc(frame, &printername, lpq_command, &size)) {
1109 goto out;
1111 DEBUG(5,("cups_queue_get(%s, %p, %p)\n", lpq_command, q, status));
1114 * Make sure we don't ask for passwords...
1117 cupsSetPasswordCB(cups_passwd_cb);
1120 * Try to connect to the server...
1123 if ((http = cups_connect(frame)) == NULL) {
1124 goto out;
1128 * Generate the printer URI...
1131 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s", printername);
1134 * Build an IPP_GET_JOBS request, which requires the following
1135 * attributes:
1137 * attributes-charset
1138 * attributes-natural-language
1139 * requested-attributes
1140 * printer-uri
1143 request = ippNew();
1145 ippSetOperation(request, IPP_GET_JOBS);
1146 ippSetRequestId(request, 1);
1148 language = cupsLangDefault();
1150 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1151 "attributes-charset", NULL, "utf-8");
1153 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1154 "attributes-natural-language", NULL, language->language);
1156 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1157 "requested-attributes",
1158 (sizeof(jattrs) / sizeof(jattrs[0])),
1159 NULL, jattrs);
1161 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1162 "printer-uri", NULL, uri);
1165 * Do the request and get back a response...
1168 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
1169 DEBUG(0,("Unable to get jobs for %s - %s\n", uri,
1170 ippErrorString(cupsLastError())));
1171 goto out;
1174 if (ippGetStatusCode(response) >= IPP_OK_CONFLICT) {
1175 DEBUG(0,("Unable to get jobs for %s - %s\n", uri,
1176 ippErrorString(ippGetStatusCode(response))));
1177 goto out;
1181 * Process the jobs...
1184 qcount = 0;
1185 qalloc = 0;
1186 queue = NULL;
1188 for (attr = ippFirstAttribute(response); attr != NULL; attr = ippNextAttribute(response)) {
1190 * Skip leading attributes until we hit a job...
1193 while (attr != NULL && ippGetGroupTag(attr) != IPP_TAG_JOB)
1194 attr = ippNextAttribute(response);
1196 if (attr == NULL)
1197 break;
1200 * Allocate memory as needed...
1202 if (qcount >= qalloc) {
1203 qalloc += 16;
1205 queue = SMB_REALLOC_ARRAY(queue, print_queue_struct, qalloc);
1207 if (queue == NULL) {
1208 DEBUG(0,("cups_queue_get: Not enough memory!"));
1209 qcount = 0;
1210 goto out;
1214 temp = queue + qcount;
1215 memset(temp, 0, sizeof(print_queue_struct));
1218 * Pull the needed attributes from this job...
1221 job_id = 0;
1222 job_priority = 50;
1223 job_status = IPP_JOB_PENDING;
1224 job_time = 0;
1225 job_k_octets = 0;
1226 user_name = NULL;
1227 job_name = NULL;
1229 while (attr != NULL && ippGetGroupTag(attr) == IPP_TAG_JOB) {
1230 if (ippGetName(attr) == NULL) {
1231 attr = ippNextAttribute(response);
1232 break;
1235 if (strcmp(ippGetName(attr), "job-id") == 0 &&
1236 ippGetValueTag(attr) == IPP_TAG_INTEGER)
1237 job_id = ippGetInteger(attr, 0);
1239 if (strcmp(ippGetName(attr), "job-k-octets") == 0 &&
1240 ippGetValueTag(attr) == IPP_TAG_INTEGER)
1241 job_k_octets = ippGetInteger(attr, 0);
1243 if (strcmp(ippGetName(attr), "job-priority") == 0 &&
1244 ippGetValueTag(attr) == IPP_TAG_INTEGER)
1245 job_priority = ippGetInteger(attr, 0);
1247 if (strcmp(ippGetName(attr), "job-state") == 0 &&
1248 ippGetValueTag(attr) == IPP_TAG_ENUM)
1249 job_status = (ipp_jstate_t)ippGetInteger(attr, 0);
1251 if (strcmp(ippGetName(attr), "time-at-creation") == 0 &&
1252 ippGetValueTag(attr) == IPP_TAG_INTEGER)
1253 job_time = ippGetInteger(attr, 0);
1255 if (strcmp(ippGetName(attr), "job-name") == 0 &&
1256 ippGetValueTag(attr) == IPP_TAG_NAME) {
1257 if (!pull_utf8_talloc(frame,
1258 &job_name,
1259 ippGetString(attr, 0, NULL),
1260 &size)) {
1261 goto out;
1265 if (strcmp(ippGetName(attr), "job-originating-user-name") == 0 &&
1266 ippGetValueTag(attr) == IPP_TAG_NAME) {
1267 if (!pull_utf8_talloc(frame,
1268 &user_name,
1269 ippGetString(attr, 0, NULL),
1270 &size)) {
1271 goto out;
1275 attr = ippNextAttribute(response);
1279 * See if we have everything needed...
1282 if (user_name == NULL || job_name == NULL || job_id == 0) {
1283 if (attr == NULL)
1284 break;
1285 else
1286 continue;
1289 temp->sysjob = job_id;
1290 temp->size = job_k_octets * 1024;
1291 temp->status = job_status == IPP_JOB_PENDING ? LPQ_QUEUED :
1292 job_status == IPP_JOB_STOPPED ? LPQ_PAUSED :
1293 job_status == IPP_JOB_HELD ? LPQ_PAUSED :
1294 LPQ_PRINTING;
1295 temp->priority = job_priority;
1296 temp->time = job_time;
1297 strlcpy(temp->fs_user, user_name, sizeof(temp->fs_user));
1298 strlcpy(temp->fs_file, job_name, sizeof(temp->fs_file));
1300 qcount ++;
1302 if (attr == NULL)
1303 break;
1306 ippDelete(response);
1307 response = NULL;
1310 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the
1311 * following attributes:
1313 * attributes-charset
1314 * attributes-natural-language
1315 * requested-attributes
1316 * printer-uri
1319 request = ippNew();
1321 ippSetOperation(request, IPP_GET_PRINTER_ATTRIBUTES);
1322 ippSetRequestId(request, 1);
1324 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1325 "attributes-charset", NULL, "utf-8");
1327 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1328 "attributes-natural-language", NULL, language->language);
1330 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1331 "requested-attributes",
1332 (sizeof(pattrs) / sizeof(pattrs[0])),
1333 NULL, pattrs);
1335 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1336 "printer-uri", NULL, uri);
1339 * Do the request and get back a response...
1342 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
1343 DEBUG(0,("Unable to get printer status for %s - %s\n", printername,
1344 ippErrorString(cupsLastError())));
1345 goto out;
1348 if (ippGetStatusCode(response) >= IPP_OK_CONFLICT) {
1349 DEBUG(0,("Unable to get printer status for %s - %s\n", printername,
1350 ippErrorString(ippGetStatusCode(response))));
1351 goto out;
1355 * Get the current printer status and convert it to the SAMBA values.
1358 if ((attr = ippFindAttribute(response, "printer-state", IPP_TAG_ENUM)) != NULL) {
1359 if (ippGetInteger(attr, 0) == IPP_PRINTER_STOPPED)
1360 status->status = LPSTAT_STOPPED;
1361 else
1362 status->status = LPSTAT_OK;
1365 if ((attr = ippFindAttribute(response, "printer-state-message",
1366 IPP_TAG_TEXT)) != NULL) {
1367 char *msg = NULL;
1368 if (!pull_utf8_talloc(frame, &msg,
1369 ippGetString(attr, 0, NULL),
1370 &size)) {
1371 SAFE_FREE(queue);
1372 qcount = 0;
1373 goto out;
1375 fstrcpy(status->message, msg);
1378 out:
1381 * Return the job queue...
1384 *q = queue;
1386 if (response)
1387 ippDelete(response);
1389 if (language)
1390 cupsLangFree(language);
1392 if (http)
1393 httpClose(http);
1395 TALLOC_FREE(frame);
1396 return qcount;
1401 * 'cups_queue_pause()' - Pause a print queue.
1404 static int cups_queue_pause(int snum)
1406 TALLOC_CTX *frame = talloc_stackframe();
1407 int ret = 1; /* Return value */
1408 http_t *http = NULL; /* HTTP connection to server */
1409 ipp_t *request = NULL, /* IPP Request */
1410 *response = NULL; /* IPP Response */
1411 cups_lang_t *language = NULL; /* Default language */
1412 char *printername = NULL;
1413 char *username = NULL;
1414 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
1415 size_t size;
1417 DEBUG(5,("cups_queue_pause(%d)\n", snum));
1420 * Make sure we don't ask for passwords...
1423 cupsSetPasswordCB(cups_passwd_cb);
1426 * Try to connect to the server...
1429 if ((http = cups_connect(frame)) == NULL) {
1430 goto out;
1434 * Build an IPP_PAUSE_PRINTER request, which requires the following
1435 * attributes:
1437 * attributes-charset
1438 * attributes-natural-language
1439 * printer-uri
1440 * requesting-user-name
1443 request = ippNew();
1445 ippSetOperation(request, IPP_PAUSE_PRINTER);
1446 ippSetRequestId(request, 1);
1448 language = cupsLangDefault();
1450 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1451 "attributes-charset", NULL, "utf-8");
1453 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1454 "attributes-natural-language", NULL, language->language);
1456 if (!push_utf8_talloc(frame, &printername, lp_printername(snum),
1457 &size)) {
1458 goto out;
1460 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s",
1461 printername);
1463 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
1465 if (!push_utf8_talloc(frame, &username, current_user_info.unix_name, &size)) {
1466 goto out;
1468 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
1469 NULL, username);
1472 * Do the request and get back a response...
1475 if ((response = cupsDoRequest(http, request, "/admin/")) != NULL) {
1476 if (ippGetStatusCode(response) >= IPP_OK_CONFLICT) {
1477 DEBUG(0,("Unable to pause printer %s - %s\n",
1478 lp_printername(snum),
1479 ippErrorString(cupsLastError())));
1480 } else {
1481 ret = 0;
1483 } else {
1484 DEBUG(0,("Unable to pause printer %s - %s\n",
1485 lp_printername(snum),
1486 ippErrorString(cupsLastError())));
1489 out:
1490 if (response)
1491 ippDelete(response);
1493 if (language)
1494 cupsLangFree(language);
1496 if (http)
1497 httpClose(http);
1499 TALLOC_FREE(frame);
1500 return ret;
1505 * 'cups_queue_resume()' - Restart a print queue.
1508 static int cups_queue_resume(int snum)
1510 TALLOC_CTX *frame = talloc_stackframe();
1511 int ret = 1; /* Return value */
1512 http_t *http = NULL; /* HTTP connection to server */
1513 ipp_t *request = NULL, /* IPP Request */
1514 *response = NULL; /* IPP Response */
1515 cups_lang_t *language = NULL; /* Default language */
1516 char *printername = NULL;
1517 char *username = NULL;
1518 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
1519 size_t size;
1521 DEBUG(5,("cups_queue_resume(%d)\n", snum));
1524 * Make sure we don't ask for passwords...
1527 cupsSetPasswordCB(cups_passwd_cb);
1530 * Try to connect to the server...
1533 if ((http = cups_connect(frame)) == NULL) {
1534 goto out;
1538 * Build an IPP_RESUME_PRINTER request, which requires the following
1539 * attributes:
1541 * attributes-charset
1542 * attributes-natural-language
1543 * printer-uri
1544 * requesting-user-name
1547 request = ippNew();
1549 ippSetOperation(request, IPP_RESUME_PRINTER);
1550 ippSetRequestId(request, 1);
1552 language = cupsLangDefault();
1554 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1555 "attributes-charset", NULL, "utf-8");
1557 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1558 "attributes-natural-language", NULL, language->language);
1560 if (!push_utf8_talloc(frame, &printername, lp_printername(snum),
1561 &size)) {
1562 goto out;
1564 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s",
1565 printername);
1567 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
1569 if (!push_utf8_talloc(frame, &username, current_user_info.unix_name, &size)) {
1570 goto out;
1572 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
1573 NULL, username);
1576 * Do the request and get back a response...
1579 if ((response = cupsDoRequest(http, request, "/admin/")) != NULL) {
1580 if (ippGetStatusCode(response) >= IPP_OK_CONFLICT) {
1581 DEBUG(0,("Unable to resume printer %s - %s\n",
1582 lp_printername(snum),
1583 ippErrorString(cupsLastError())));
1584 } else {
1585 ret = 0;
1587 } else {
1588 DEBUG(0,("Unable to resume printer %s - %s\n",
1589 lp_printername(snum),
1590 ippErrorString(cupsLastError())));
1593 out:
1594 if (response)
1595 ippDelete(response);
1597 if (language)
1598 cupsLangFree(language);
1600 if (http)
1601 httpClose(http);
1603 TALLOC_FREE(frame);
1604 return ret;
1607 /*******************************************************************
1608 * CUPS printing interface definitions...
1609 ******************************************************************/
1611 struct printif cups_printif =
1613 PRINT_CUPS,
1614 cups_queue_get,
1615 cups_queue_pause,
1616 cups_queue_resume,
1617 cups_job_delete,
1618 cups_job_pause,
1619 cups_job_resume,
1620 cups_job_submit,
1623 #else
1624 /* this keeps fussy compilers happy */
1625 void print_cups_dummy(void);
1626 void print_cups_dummy(void) {}
1627 #endif /* HAVE_CUPS */