Document default of the printing config variable.
[Samba.git] / source / printing / print_cups.c
blobde7bcf5fe56d7edd204974b38d560f5e572c819d
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"
28 #ifdef HAVE_CUPS
29 #include <cups/cups.h>
30 #include <cups/language.h>
32 extern userdom_struct current_user_info;
35 * 'cups_passwd_cb()' - The CUPS password callback...
38 static const char * /* O - Password or NULL */
39 cups_passwd_cb(const char *prompt) /* I - Prompt */
42 * Always return NULL to indicate that no password is available...
45 return (NULL);
48 static http_t *cups_connect(TALLOC_CTX *frame)
50 http_t *http = NULL;
51 char *server = NULL, *p = NULL;
52 int port;
54 if (lp_cups_server() != NULL && strlen(lp_cups_server()) > 0) {
55 if (push_utf8_talloc(frame, &server, lp_cups_server()) == (size_t)-1) {
56 return NULL;
58 } else {
59 server = talloc_strdup(frame,cupsServer());
61 if (!server) {
62 return NULL;
65 p = strchr(server, ':');
66 if (p) {
67 port = atoi(p+1);
68 *p = '\0';
69 } else {
70 port = ippPort();
73 DEBUG(10, ("connecting to cups server %s:%d\n",
74 server, port));
76 if ((http = httpConnect(server, port)) == NULL) {
77 DEBUG(0,("Unable to connect to CUPS server %s:%d - %s\n",
78 server, port, strerror(errno)));
79 return NULL;
82 return http;
85 static void send_pcap_info(const char *name, const char *info, void *pd)
87 int fd = *(int *)pd;
88 size_t namelen = name ? strlen(name)+1 : 0;
89 size_t infolen = info ? strlen(info)+1 : 0;
91 DEBUG(11,("send_pcap_info: writing namelen %u\n", (unsigned int)namelen));
92 if (sys_write(fd, &namelen, sizeof(namelen)) != sizeof(namelen)) {
93 DEBUG(10,("send_pcap_info: namelen write failed %s\n",
94 strerror(errno)));
95 return;
97 DEBUG(11,("send_pcap_info: writing infolen %u\n", (unsigned int)infolen));
98 if (sys_write(fd, &infolen, sizeof(infolen)) != sizeof(infolen)) {
99 DEBUG(10,("send_pcap_info: infolen write failed %s\n",
100 strerror(errno)));
101 return;
103 if (namelen) {
104 DEBUG(11,("send_pcap_info: writing name %s\n", name));
105 if (sys_write(fd, name, namelen) != namelen) {
106 DEBUG(10,("send_pcap_info: name write failed %s\n",
107 strerror(errno)));
108 return;
111 if (infolen) {
112 DEBUG(11,("send_pcap_info: writing info %s\n", info));
113 if (sys_write(fd, info, infolen) != infolen) {
114 DEBUG(10,("send_pcap_info: info write failed %s\n",
115 strerror(errno)));
116 return;
121 static bool cups_cache_reload_async(int fd)
123 TALLOC_CTX *frame = talloc_stackframe();
124 struct pcap_cache *tmp_pcap_cache = NULL;
125 http_t *http = NULL; /* HTTP connection to server */
126 ipp_t *request = NULL, /* IPP Request */
127 *response = NULL; /* IPP Response */
128 ipp_attribute_t *attr; /* Current attribute */
129 cups_lang_t *language = NULL; /* Default language */
130 char *name, /* printer-name attribute */
131 *info; /* printer-info attribute */
132 static const char *requested[] =/* Requested attributes */
134 "printer-name",
135 "printer-info"
137 bool ret = False;
139 DEBUG(5, ("reloading cups printcap cache\n"));
142 * Make sure we don't ask for passwords...
145 cupsSetPasswordCB(cups_passwd_cb);
148 * Try to connect to the server...
151 if ((http = cups_connect(frame)) == NULL) {
152 goto out;
156 * Build a CUPS_GET_PRINTERS request, which requires the following
157 * attributes:
159 * attributes-charset
160 * attributes-natural-language
161 * requested-attributes
164 request = ippNew();
166 request->request.op.operation_id = CUPS_GET_PRINTERS;
167 request->request.op.request_id = 1;
169 language = cupsLangDefault();
171 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
172 "attributes-charset", NULL, "utf-8");
174 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
175 "attributes-natural-language", NULL, language->language);
177 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
178 "requested-attributes",
179 (sizeof(requested) / sizeof(requested[0])),
180 NULL, requested);
183 * Do the request and get back a response...
186 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
187 DEBUG(0,("Unable to get printer list - %s\n",
188 ippErrorString(cupsLastError())));
189 goto out;
192 for (attr = response->attrs; attr != NULL;) {
194 * Skip leading attributes until we hit a printer...
197 while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)
198 attr = attr->next;
200 if (attr == NULL)
201 break;
204 * Pull the needed attributes from this printer...
207 name = NULL;
208 info = NULL;
210 while (attr != NULL && attr->group_tag == IPP_TAG_PRINTER) {
211 if (strcmp(attr->name, "printer-name") == 0 &&
212 attr->value_tag == IPP_TAG_NAME) {
213 pull_utf8_talloc(frame,
214 &name,
215 attr->values[0].string.text);
218 if (strcmp(attr->name, "printer-info") == 0 &&
219 attr->value_tag == IPP_TAG_TEXT) {
220 pull_utf8_talloc(frame,
221 &info,
222 attr->values[0].string.text);
225 attr = attr->next;
229 * See if we have everything needed...
232 if (name == NULL)
233 break;
235 if (!pcap_cache_add_specific(&tmp_pcap_cache, name, info)) {
236 goto out;
240 ippDelete(response);
241 response = NULL;
244 * Build a CUPS_GET_CLASSES request, which requires the following
245 * attributes:
247 * attributes-charset
248 * attributes-natural-language
249 * requested-attributes
252 request = ippNew();
254 request->request.op.operation_id = CUPS_GET_CLASSES;
255 request->request.op.request_id = 1;
257 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
258 "attributes-charset", NULL, "utf-8");
260 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
261 "attributes-natural-language", NULL, language->language);
263 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
264 "requested-attributes",
265 (sizeof(requested) / sizeof(requested[0])),
266 NULL, requested);
269 * Do the request and get back a response...
272 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
273 DEBUG(0,("Unable to get printer list - %s\n",
274 ippErrorString(cupsLastError())));
275 goto out;
278 for (attr = response->attrs; attr != NULL;) {
280 * Skip leading attributes until we hit a printer...
283 while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)
284 attr = attr->next;
286 if (attr == NULL)
287 break;
290 * Pull the needed attributes from this printer...
293 name = NULL;
294 info = NULL;
296 while (attr != NULL && attr->group_tag == IPP_TAG_PRINTER) {
297 if (strcmp(attr->name, "printer-name") == 0 &&
298 attr->value_tag == IPP_TAG_NAME) {
299 pull_utf8_talloc(frame,
300 &name,
301 attr->values[0].string.text);
304 if (strcmp(attr->name, "printer-info") == 0 &&
305 attr->value_tag == IPP_TAG_TEXT) {
306 pull_utf8_talloc(frame,
307 &info,
308 attr->values[0].string.text);
311 attr = attr->next;
315 * See if we have everything needed...
318 if (name == NULL)
319 break;
321 if (!pcap_cache_add_specific(&tmp_pcap_cache, name, info)) {
322 goto out;
326 ret = True;
328 out:
329 if (response)
330 ippDelete(response);
332 if (language)
333 cupsLangFree(language);
335 if (http)
336 httpClose(http);
338 /* Send all the entries up the pipe. */
339 if (tmp_pcap_cache) {
340 pcap_printer_fn_specific(tmp_pcap_cache,
341 send_pcap_info,
342 (void *)&fd);
344 pcap_cache_destroy_specific(&tmp_pcap_cache);
346 TALLOC_FREE(frame);
347 return ret;
350 static struct pcap_cache *local_pcap_copy;
351 struct fd_event *cache_fd_event;
353 static bool cups_pcap_load_async(int *pfd)
355 int fds[2];
356 pid_t pid;
358 *pfd = -1;
360 if (cache_fd_event) {
361 DEBUG(3,("cups_pcap_load_async: already waiting for "
362 "a refresh event\n" ));
363 return false;
366 DEBUG(5,("cups_pcap_load_async: asynchronously loading cups printers\n"));
368 if (pipe(fds) == -1) {
369 return false;
372 pid = sys_fork();
373 if (pid == (pid_t)-1) {
374 DEBUG(10,("cups_pcap_load_async: fork failed %s\n",
375 strerror(errno) ));
376 close(fds[0]);
377 close(fds[1]);
378 return false;
381 if (pid) {
382 DEBUG(10,("cups_pcap_load_async: child pid = %u\n",
383 (unsigned int)pid ));
384 /* Parent. */
385 close(fds[1]);
386 *pfd = fds[0];
387 return true;
390 /* Child. */
391 close_all_print_db();
393 if (!reinit_after_fork(smbd_messaging_context(),
394 smbd_event_context(), true)) {
395 DEBUG(0,("cups_pcap_load_async: reinit_after_fork() failed\n"));
396 smb_panic("cups_pcap_load_async: reinit_after_fork() failed");
399 close(fds[0]);
400 cups_cache_reload_async(fds[1]);
401 close(fds[1]);
402 _exit(0);
405 static void cups_async_callback(struct event_context *event_ctx,
406 struct fd_event *event,
407 uint16 flags,
408 void *p)
410 TALLOC_CTX *frame = talloc_stackframe();
411 int fd = *(int *)p;
412 struct pcap_cache *tmp_pcap_cache = NULL;
414 DEBUG(5,("cups_async_callback: callback received for printer data. "
415 "fd = %d\n", fd));
417 while (1) {
418 char *name = NULL, *info = NULL;
419 size_t namelen = 0, infolen = 0;
420 ssize_t ret = -1;
422 ret = sys_read(fd, &namelen, sizeof(namelen));
423 if (ret == 0) {
424 /* EOF */
425 break;
427 if (ret != sizeof(namelen)) {
428 DEBUG(10,("cups_async_callback: namelen read failed %d %s\n",
429 errno, strerror(errno)));
430 break;
433 DEBUG(11,("cups_async_callback: read namelen %u\n",
434 (unsigned int)namelen));
436 ret = sys_read(fd, &infolen, sizeof(infolen));
437 if (ret == 0) {
438 /* EOF */
439 break;
441 if (ret != sizeof(infolen)) {
442 DEBUG(10,("cups_async_callback: infolen read failed %s\n",
443 strerror(errno)));
444 break;
447 DEBUG(11,("cups_async_callback: read infolen %u\n",
448 (unsigned int)infolen));
450 if (namelen) {
451 name = TALLOC_ARRAY(frame, char, namelen);
452 if (!name) {
453 break;
455 ret = sys_read(fd, name, namelen);
456 if (ret == 0) {
457 /* EOF */
458 break;
460 if (ret != namelen) {
461 DEBUG(10,("cups_async_callback: name read failed %s\n",
462 strerror(errno)));
463 break;
465 DEBUG(11,("cups_async_callback: read name %s\n",
466 name));
467 } else {
468 name = NULL;
470 if (infolen) {
471 info = TALLOC_ARRAY(frame, char, infolen);
472 if (!info) {
473 break;
475 ret = sys_read(fd, info, infolen);
476 if (ret == 0) {
477 /* EOF */
478 break;
480 if (ret != infolen) {
481 DEBUG(10,("cups_async_callback: info read failed %s\n",
482 strerror(errno)));
483 break;
485 DEBUG(11,("cups_async_callback: read info %s\n",
486 info));
487 } else {
488 info = NULL;
491 /* Add to our local pcap cache. */
492 pcap_cache_add_specific(&tmp_pcap_cache, name, info);
493 TALLOC_FREE(name);
494 TALLOC_FREE(info);
497 TALLOC_FREE(frame);
498 if (tmp_pcap_cache) {
499 /* We got a namelist, replace our local cache. */
500 pcap_cache_destroy_specific(&local_pcap_copy);
501 local_pcap_copy = tmp_pcap_cache;
503 /* And the systemwide pcap cache. */
504 pcap_cache_replace(local_pcap_copy);
505 } else {
506 DEBUG(2,("cups_async_callback: failed to read a new "
507 "printer list\n"));
509 close(fd);
510 TALLOC_FREE(p);
511 TALLOC_FREE(cache_fd_event);
514 bool cups_cache_reload(void)
516 int *p_pipe_fd = TALLOC_P(NULL, int);
518 if (!p_pipe_fd) {
519 return false;
522 *p_pipe_fd = -1;
524 /* Set up an async refresh. */
525 if (!cups_pcap_load_async(p_pipe_fd)) {
526 return false;
528 if (!local_pcap_copy) {
529 /* We have no local cache, wait directly for
530 * async refresh to complete.
532 DEBUG(10,("cups_cache_reload: sync read on fd %d\n",
533 *p_pipe_fd ));
535 cups_async_callback(smbd_event_context(),
536 NULL,
537 EVENT_FD_READ,
538 (void *)p_pipe_fd);
539 if (!local_pcap_copy) {
540 return false;
542 } else {
543 /* Replace the system cache with our
544 * local copy. */
545 pcap_cache_replace(local_pcap_copy);
547 DEBUG(10,("cups_cache_reload: async read on fd %d\n",
548 *p_pipe_fd ));
550 /* Trigger an event when the pipe can be read. */
551 cache_fd_event = event_add_fd(smbd_event_context(),
552 NULL, *p_pipe_fd,
553 EVENT_FD_READ,
554 cups_async_callback,
555 (void *)p_pipe_fd);
556 if (!cache_fd_event) {
557 close(*p_pipe_fd);
558 TALLOC_FREE(p_pipe_fd);
559 return false;
562 return true;
566 * 'cups_job_delete()' - Delete a job.
569 static int cups_job_delete(const char *sharename, const char *lprm_command, struct printjob *pjob)
571 TALLOC_CTX *frame = talloc_stackframe();
572 int ret = 1; /* Return value */
573 http_t *http = NULL; /* HTTP connection to server */
574 ipp_t *request = NULL, /* IPP Request */
575 *response = NULL; /* IPP Response */
576 cups_lang_t *language = NULL; /* Default language */
577 char *user = NULL;
578 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
581 DEBUG(5,("cups_job_delete(%s, %p (%d))\n", sharename, pjob, pjob->sysjob));
584 * Make sure we don't ask for passwords...
587 cupsSetPasswordCB(cups_passwd_cb);
590 * Try to connect to the server...
593 if ((http = cups_connect(frame)) == NULL) {
594 goto out;
598 * Build an IPP_CANCEL_JOB request, which requires the following
599 * attributes:
601 * attributes-charset
602 * attributes-natural-language
603 * job-uri
604 * requesting-user-name
607 request = ippNew();
609 request->request.op.operation_id = IPP_CANCEL_JOB;
610 request->request.op.request_id = 1;
612 language = cupsLangDefault();
614 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
615 "attributes-charset", NULL, "utf-8");
617 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
618 "attributes-natural-language", NULL, language->language);
620 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/jobs/%d", pjob->sysjob);
622 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri);
624 if (push_utf8_talloc(frame, &user, pjob->user) == (size_t)-1) {
625 goto out;
628 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
629 NULL, user);
632 * Do the request and get back a response...
635 if ((response = cupsDoRequest(http, request, "/jobs")) != NULL) {
636 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
637 DEBUG(0,("Unable to cancel job %d - %s\n", pjob->sysjob,
638 ippErrorString(cupsLastError())));
639 } else {
640 ret = 0;
642 } else {
643 DEBUG(0,("Unable to cancel job %d - %s\n", pjob->sysjob,
644 ippErrorString(cupsLastError())));
647 out:
648 if (response)
649 ippDelete(response);
651 if (language)
652 cupsLangFree(language);
654 if (http)
655 httpClose(http);
657 TALLOC_FREE(frame);
658 return ret;
663 * 'cups_job_pause()' - Pause a job.
666 static int cups_job_pause(int snum, struct printjob *pjob)
668 TALLOC_CTX *frame = talloc_stackframe();
669 int ret = 1; /* Return value */
670 http_t *http = NULL; /* HTTP connection to server */
671 ipp_t *request = NULL, /* IPP Request */
672 *response = NULL; /* IPP Response */
673 cups_lang_t *language = NULL; /* Default language */
674 char *user = NULL;
675 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
678 DEBUG(5,("cups_job_pause(%d, %p (%d))\n", snum, pjob, pjob->sysjob));
681 * Make sure we don't ask for passwords...
684 cupsSetPasswordCB(cups_passwd_cb);
687 * Try to connect to the server...
690 if ((http = cups_connect(frame)) == NULL) {
691 goto out;
695 * Build an IPP_HOLD_JOB request, which requires the following
696 * attributes:
698 * attributes-charset
699 * attributes-natural-language
700 * job-uri
701 * requesting-user-name
704 request = ippNew();
706 request->request.op.operation_id = IPP_HOLD_JOB;
707 request->request.op.request_id = 1;
709 language = cupsLangDefault();
711 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
712 "attributes-charset", NULL, "utf-8");
714 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
715 "attributes-natural-language", NULL, language->language);
717 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/jobs/%d", pjob->sysjob);
719 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri);
721 if (push_utf8_talloc(frame, &user, pjob->user) == (size_t)-1) {
722 goto out;
724 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
725 NULL, user);
728 * Do the request and get back a response...
731 if ((response = cupsDoRequest(http, request, "/jobs")) != NULL) {
732 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
733 DEBUG(0,("Unable to hold job %d - %s\n", pjob->sysjob,
734 ippErrorString(cupsLastError())));
735 } else {
736 ret = 0;
738 } else {
739 DEBUG(0,("Unable to hold job %d - %s\n", pjob->sysjob,
740 ippErrorString(cupsLastError())));
743 out:
744 if (response)
745 ippDelete(response);
747 if (language)
748 cupsLangFree(language);
750 if (http)
751 httpClose(http);
753 TALLOC_FREE(frame);
754 return ret;
759 * 'cups_job_resume()' - Resume a paused job.
762 static int cups_job_resume(int snum, struct printjob *pjob)
764 TALLOC_CTX *frame = talloc_stackframe();
765 int ret = 1; /* Return value */
766 http_t *http = NULL; /* HTTP connection to server */
767 ipp_t *request = NULL, /* IPP Request */
768 *response = NULL; /* IPP Response */
769 cups_lang_t *language = NULL; /* Default language */
770 char *user = NULL;
771 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
774 DEBUG(5,("cups_job_resume(%d, %p (%d))\n", snum, pjob, pjob->sysjob));
777 * Make sure we don't ask for passwords...
780 cupsSetPasswordCB(cups_passwd_cb);
783 * Try to connect to the server...
786 if ((http = cups_connect(frame)) == NULL) {
787 goto out;
791 * Build an IPP_RELEASE_JOB request, which requires the following
792 * attributes:
794 * attributes-charset
795 * attributes-natural-language
796 * job-uri
797 * requesting-user-name
800 request = ippNew();
802 request->request.op.operation_id = IPP_RELEASE_JOB;
803 request->request.op.request_id = 1;
805 language = cupsLangDefault();
807 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
808 "attributes-charset", NULL, "utf-8");
810 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
811 "attributes-natural-language", NULL, language->language);
813 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/jobs/%d", pjob->sysjob);
815 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri);
817 if (push_utf8_talloc(frame, &user, pjob->user) == (size_t)-1) {
818 goto out;
820 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
821 NULL, user);
824 * Do the request and get back a response...
827 if ((response = cupsDoRequest(http, request, "/jobs")) != NULL) {
828 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
829 DEBUG(0,("Unable to release job %d - %s\n", pjob->sysjob,
830 ippErrorString(cupsLastError())));
831 } else {
832 ret = 0;
834 } else {
835 DEBUG(0,("Unable to release job %d - %s\n", pjob->sysjob,
836 ippErrorString(cupsLastError())));
839 out:
840 if (response)
841 ippDelete(response);
843 if (language)
844 cupsLangFree(language);
846 if (http)
847 httpClose(http);
849 TALLOC_FREE(frame);
850 return ret;
855 * 'cups_job_submit()' - Submit a job for printing.
858 static int cups_job_submit(int snum, struct printjob *pjob)
860 TALLOC_CTX *frame = talloc_stackframe();
861 int ret = 1; /* Return value */
862 http_t *http = NULL; /* HTTP connection to server */
863 ipp_t *request = NULL, /* IPP Request */
864 *response = NULL; /* IPP Response */
865 cups_lang_t *language = NULL; /* Default language */
866 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
867 const char *clientname = NULL; /* hostname of client for job-originating-host attribute */
868 char *new_jobname = NULL;
869 int num_options = 0;
870 cups_option_t *options = NULL;
871 char *printername = NULL;
872 char *user = NULL;
873 char *jobname = NULL;
874 char *cupsoptions = NULL;
875 char *filename = NULL;
876 char addr[INET6_ADDRSTRLEN];
878 DEBUG(5,("cups_job_submit(%d, %p (%d))\n", snum, pjob, pjob->sysjob));
881 * Make sure we don't ask for passwords...
884 cupsSetPasswordCB(cups_passwd_cb);
887 * Try to connect to the server...
890 if ((http = cups_connect(frame)) == NULL) {
891 goto out;
895 * Build an IPP_PRINT_JOB request, which requires the following
896 * attributes:
898 * attributes-charset
899 * attributes-natural-language
900 * printer-uri
901 * requesting-user-name
902 * [document-data]
905 request = ippNew();
907 request->request.op.operation_id = IPP_PRINT_JOB;
908 request->request.op.request_id = 1;
910 language = cupsLangDefault();
912 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
913 "attributes-charset", NULL, "utf-8");
915 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
916 "attributes-natural-language", NULL, language->language);
918 if (push_utf8_talloc(frame, &printername, PRINTERNAME(snum)) == (size_t)-1) {
919 goto out;
921 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s",
922 printername);
924 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
925 "printer-uri", NULL, uri);
927 if (push_utf8_talloc(frame, &user, pjob->user) == (size_t)-1) {
928 goto out;
930 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
931 NULL, user);
933 clientname = client_name(get_client_fd());
934 if (strcmp(clientname, "UNKNOWN") == 0) {
935 clientname = client_addr(get_client_fd(),addr,sizeof(addr));
938 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
939 "job-originating-host-name", NULL,
940 clientname);
942 if (push_utf8_talloc(frame, &jobname, pjob->jobname) == (size_t)-1) {
943 goto out;
945 new_jobname = talloc_asprintf(frame,
946 "%s%.8u %s", PRINT_SPOOL_PREFIX,
947 (unsigned int)pjob->smbjob,
948 jobname);
949 if (new_jobname == NULL) {
950 goto out;
953 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name", NULL,
954 new_jobname);
957 * add any options defined in smb.conf
960 if (push_utf8_talloc(frame, &cupsoptions, lp_cups_options(snum)) == (size_t)-1) {
961 goto out;
963 num_options = 0;
964 options = NULL;
965 num_options = cupsParseOptions(cupsoptions, num_options, &options);
967 if ( num_options )
968 cupsEncodeOptions(request, num_options, options);
971 * Do the request and get back a response...
974 slprintf(uri, sizeof(uri) - 1, "/printers/%s", printername);
976 if (push_utf8_talloc(frame, &filename, pjob->filename) == (size_t)-1) {
977 goto out;
979 if ((response = cupsDoFileRequest(http, request, uri, pjob->filename)) != NULL) {
980 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
981 DEBUG(0,("Unable to print file to %s - %s\n", PRINTERNAME(snum),
982 ippErrorString(cupsLastError())));
983 } else {
984 ret = 0;
986 } else {
987 DEBUG(0,("Unable to print file to `%s' - %s\n", PRINTERNAME(snum),
988 ippErrorString(cupsLastError())));
991 if ( ret == 0 )
992 unlink(pjob->filename);
993 /* else print_job_end will do it for us */
995 out:
996 if (response)
997 ippDelete(response);
999 if (language)
1000 cupsLangFree(language);
1002 if (http)
1003 httpClose(http);
1005 TALLOC_FREE(frame);
1007 return ret;
1011 * 'cups_queue_get()' - Get all the jobs in the print queue.
1014 static int cups_queue_get(const char *sharename,
1015 enum printing_types printing_type,
1016 char *lpq_command,
1017 print_queue_struct **q,
1018 print_status_struct *status)
1020 TALLOC_CTX *frame = talloc_stackframe();
1021 char *printername = NULL;
1022 http_t *http = NULL; /* HTTP connection to server */
1023 ipp_t *request = NULL, /* IPP Request */
1024 *response = NULL; /* IPP Response */
1025 ipp_attribute_t *attr = NULL; /* Current attribute */
1026 cups_lang_t *language = NULL; /* Default language */
1027 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
1028 int qcount = 0, /* Number of active queue entries */
1029 qalloc = 0; /* Number of queue entries allocated */
1030 print_queue_struct *queue = NULL, /* Queue entries */
1031 *temp; /* Temporary pointer for queue */
1032 char *user_name = NULL, /* job-originating-user-name attribute */
1033 *job_name = NULL; /* job-name attribute */
1034 int job_id; /* job-id attribute */
1035 int job_k_octets; /* job-k-octets attribute */
1036 time_t job_time; /* time-at-creation attribute */
1037 ipp_jstate_t job_status; /* job-status attribute */
1038 int job_priority; /* job-priority attribute */
1039 static const char *jattrs[] = /* Requested job attributes */
1041 "job-id",
1042 "job-k-octets",
1043 "job-name",
1044 "job-originating-user-name",
1045 "job-priority",
1046 "job-state",
1047 "time-at-creation",
1049 static const char *pattrs[] = /* Requested printer attributes */
1051 "printer-state",
1052 "printer-state-message"
1055 *q = NULL;
1057 /* HACK ALERT!!! The problem with support the 'printer name'
1058 option is that we key the tdb off the sharename. So we will
1059 overload the lpq_command string to pass in the printername
1060 (which is basically what we do for non-cups printers ... using
1061 the lpq_command to get the queue listing). */
1063 if (push_utf8_talloc(frame, &printername, lpq_command) == (size_t)-1) {
1064 goto out;
1066 DEBUG(5,("cups_queue_get(%s, %p, %p)\n", lpq_command, q, status));
1069 * Make sure we don't ask for passwords...
1072 cupsSetPasswordCB(cups_passwd_cb);
1075 * Try to connect to the server...
1078 if ((http = cups_connect(frame)) == NULL) {
1079 goto out;
1083 * Generate the printer URI...
1086 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s", printername);
1089 * Build an IPP_GET_JOBS request, which requires the following
1090 * attributes:
1092 * attributes-charset
1093 * attributes-natural-language
1094 * requested-attributes
1095 * printer-uri
1098 request = ippNew();
1100 request->request.op.operation_id = IPP_GET_JOBS;
1101 request->request.op.request_id = 1;
1103 language = cupsLangDefault();
1105 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1106 "attributes-charset", NULL, "utf-8");
1108 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1109 "attributes-natural-language", NULL, language->language);
1111 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1112 "requested-attributes",
1113 (sizeof(jattrs) / sizeof(jattrs[0])),
1114 NULL, jattrs);
1116 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1117 "printer-uri", NULL, uri);
1120 * Do the request and get back a response...
1123 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
1124 DEBUG(0,("Unable to get jobs for %s - %s\n", uri,
1125 ippErrorString(cupsLastError())));
1126 goto out;
1129 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1130 DEBUG(0,("Unable to get jobs for %s - %s\n", uri,
1131 ippErrorString(response->request.status.status_code)));
1132 goto out;
1136 * Process the jobs...
1139 qcount = 0;
1140 qalloc = 0;
1141 queue = NULL;
1143 for (attr = response->attrs; attr != NULL; attr = attr->next) {
1145 * Skip leading attributes until we hit a job...
1148 while (attr != NULL && attr->group_tag != IPP_TAG_JOB)
1149 attr = attr->next;
1151 if (attr == NULL)
1152 break;
1155 * Allocate memory as needed...
1157 if (qcount >= qalloc) {
1158 qalloc += 16;
1160 queue = SMB_REALLOC_ARRAY(queue, print_queue_struct, qalloc);
1162 if (queue == NULL) {
1163 DEBUG(0,("cups_queue_get: Not enough memory!"));
1164 qcount = 0;
1165 goto out;
1169 temp = queue + qcount;
1170 memset(temp, 0, sizeof(print_queue_struct));
1173 * Pull the needed attributes from this job...
1176 job_id = 0;
1177 job_priority = 50;
1178 job_status = IPP_JOB_PENDING;
1179 job_time = 0;
1180 job_k_octets = 0;
1181 user_name = NULL;
1182 job_name = NULL;
1184 while (attr != NULL && attr->group_tag == IPP_TAG_JOB) {
1185 if (attr->name == NULL) {
1186 attr = attr->next;
1187 break;
1190 if (strcmp(attr->name, "job-id") == 0 &&
1191 attr->value_tag == IPP_TAG_INTEGER)
1192 job_id = attr->values[0].integer;
1194 if (strcmp(attr->name, "job-k-octets") == 0 &&
1195 attr->value_tag == IPP_TAG_INTEGER)
1196 job_k_octets = attr->values[0].integer;
1198 if (strcmp(attr->name, "job-priority") == 0 &&
1199 attr->value_tag == IPP_TAG_INTEGER)
1200 job_priority = attr->values[0].integer;
1202 if (strcmp(attr->name, "job-state") == 0 &&
1203 attr->value_tag == IPP_TAG_ENUM)
1204 job_status = (ipp_jstate_t)(attr->values[0].integer);
1206 if (strcmp(attr->name, "time-at-creation") == 0 &&
1207 attr->value_tag == IPP_TAG_INTEGER)
1208 job_time = attr->values[0].integer;
1210 if (strcmp(attr->name, "job-name") == 0 &&
1211 attr->value_tag == IPP_TAG_NAME) {
1212 pull_utf8_talloc(frame,
1213 &job_name,
1214 attr->values[0].string.text);
1217 if (strcmp(attr->name, "job-originating-user-name") == 0 &&
1218 attr->value_tag == IPP_TAG_NAME) {
1219 pull_utf8_talloc(frame,
1220 &user_name,
1221 attr->values[0].string.text);
1224 attr = attr->next;
1228 * See if we have everything needed...
1231 if (user_name == NULL || job_name == NULL || job_id == 0) {
1232 if (attr == NULL)
1233 break;
1234 else
1235 continue;
1238 temp->job = job_id;
1239 temp->size = job_k_octets * 1024;
1240 temp->status = job_status == IPP_JOB_PENDING ? LPQ_QUEUED :
1241 job_status == IPP_JOB_STOPPED ? LPQ_PAUSED :
1242 job_status == IPP_JOB_HELD ? LPQ_PAUSED :
1243 LPQ_PRINTING;
1244 temp->priority = job_priority;
1245 temp->time = job_time;
1246 strlcpy(temp->fs_user, user_name, sizeof(temp->fs_user));
1247 strlcpy(temp->fs_file, job_name, sizeof(temp->fs_file));
1249 qcount ++;
1251 if (attr == NULL)
1252 break;
1255 ippDelete(response);
1256 response = NULL;
1259 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the
1260 * following attributes:
1262 * attributes-charset
1263 * attributes-natural-language
1264 * requested-attributes
1265 * printer-uri
1268 request = ippNew();
1270 request->request.op.operation_id = IPP_GET_PRINTER_ATTRIBUTES;
1271 request->request.op.request_id = 1;
1273 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1274 "attributes-charset", NULL, "utf-8");
1276 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1277 "attributes-natural-language", NULL, language->language);
1279 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1280 "requested-attributes",
1281 (sizeof(pattrs) / sizeof(pattrs[0])),
1282 NULL, pattrs);
1284 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1285 "printer-uri", NULL, uri);
1288 * Do the request and get back a response...
1291 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
1292 DEBUG(0,("Unable to get printer status for %s - %s\n", printername,
1293 ippErrorString(cupsLastError())));
1294 *q = queue;
1295 goto out;
1298 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1299 DEBUG(0,("Unable to get printer status for %s - %s\n", printername,
1300 ippErrorString(response->request.status.status_code)));
1301 *q = queue;
1302 goto out;
1306 * Get the current printer status and convert it to the SAMBA values.
1309 if ((attr = ippFindAttribute(response, "printer-state", IPP_TAG_ENUM)) != NULL) {
1310 if (attr->values[0].integer == IPP_PRINTER_STOPPED)
1311 status->status = LPSTAT_STOPPED;
1312 else
1313 status->status = LPSTAT_OK;
1316 if ((attr = ippFindAttribute(response, "printer-state-message",
1317 IPP_TAG_TEXT)) != NULL) {
1318 char *msg = NULL;
1319 pull_utf8_talloc(frame, &msg, attr->values[0].string.text);
1320 fstrcpy(status->message, msg);
1324 * Return the job queue...
1327 *q = queue;
1329 out:
1330 if (response)
1331 ippDelete(response);
1333 if (language)
1334 cupsLangFree(language);
1336 if (http)
1337 httpClose(http);
1339 TALLOC_FREE(frame);
1340 return qcount;
1345 * 'cups_queue_pause()' - Pause a print queue.
1348 static int cups_queue_pause(int snum)
1350 TALLOC_CTX *frame = talloc_stackframe();
1351 int ret = 1; /* Return value */
1352 http_t *http = NULL; /* HTTP connection to server */
1353 ipp_t *request = NULL, /* IPP Request */
1354 *response = NULL; /* IPP Response */
1355 cups_lang_t *language = NULL; /* Default language */
1356 char *printername = NULL;
1357 char *username = NULL;
1358 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
1361 DEBUG(5,("cups_queue_pause(%d)\n", snum));
1364 * Make sure we don't ask for passwords...
1367 cupsSetPasswordCB(cups_passwd_cb);
1370 * Try to connect to the server...
1373 if ((http = cups_connect(frame)) == NULL) {
1374 goto out;
1378 * Build an IPP_PAUSE_PRINTER request, which requires the following
1379 * attributes:
1381 * attributes-charset
1382 * attributes-natural-language
1383 * printer-uri
1384 * requesting-user-name
1387 request = ippNew();
1389 request->request.op.operation_id = IPP_PAUSE_PRINTER;
1390 request->request.op.request_id = 1;
1392 language = cupsLangDefault();
1394 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1395 "attributes-charset", NULL, "utf-8");
1397 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1398 "attributes-natural-language", NULL, language->language);
1400 if (push_utf8_talloc(frame, &printername, PRINTERNAME(snum)) == (size_t)-1) {
1401 goto out;
1403 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s",
1404 printername);
1406 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
1408 if (push_utf8_talloc(frame, &username, current_user_info.unix_name) == (size_t)-1) {
1409 goto out;
1411 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
1412 NULL, username);
1415 * Do the request and get back a response...
1418 if ((response = cupsDoRequest(http, request, "/admin/")) != NULL) {
1419 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1420 DEBUG(0,("Unable to pause printer %s - %s\n", PRINTERNAME(snum),
1421 ippErrorString(cupsLastError())));
1422 } else {
1423 ret = 0;
1425 } else {
1426 DEBUG(0,("Unable to pause printer %s - %s\n", PRINTERNAME(snum),
1427 ippErrorString(cupsLastError())));
1430 out:
1431 if (response)
1432 ippDelete(response);
1434 if (language)
1435 cupsLangFree(language);
1437 if (http)
1438 httpClose(http);
1440 TALLOC_FREE(frame);
1441 return ret;
1446 * 'cups_queue_resume()' - Restart a print queue.
1449 static int cups_queue_resume(int snum)
1451 TALLOC_CTX *frame = talloc_stackframe();
1452 int ret = 1; /* Return value */
1453 http_t *http = NULL; /* HTTP connection to server */
1454 ipp_t *request = NULL, /* IPP Request */
1455 *response = NULL; /* IPP Response */
1456 cups_lang_t *language = NULL; /* Default language */
1457 char *printername = NULL;
1458 char *username = NULL;
1459 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
1462 DEBUG(5,("cups_queue_resume(%d)\n", snum));
1465 * Make sure we don't ask for passwords...
1468 cupsSetPasswordCB(cups_passwd_cb);
1471 * Try to connect to the server...
1474 if ((http = cups_connect(frame)) == NULL) {
1475 goto out;
1479 * Build an IPP_RESUME_PRINTER request, which requires the following
1480 * attributes:
1482 * attributes-charset
1483 * attributes-natural-language
1484 * printer-uri
1485 * requesting-user-name
1488 request = ippNew();
1490 request->request.op.operation_id = IPP_RESUME_PRINTER;
1491 request->request.op.request_id = 1;
1493 language = cupsLangDefault();
1495 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1496 "attributes-charset", NULL, "utf-8");
1498 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1499 "attributes-natural-language", NULL, language->language);
1501 if (push_utf8_talloc(frame, &printername, PRINTERNAME(snum)) == (size_t)-1) {
1502 goto out;
1504 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s",
1505 printername);
1507 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
1509 if (push_utf8_talloc(frame, &username, current_user_info.unix_name) == (size_t)-1) {
1510 goto out;
1512 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
1513 NULL, username);
1516 * Do the request and get back a response...
1519 if ((response = cupsDoRequest(http, request, "/admin/")) != NULL) {
1520 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1521 DEBUG(0,("Unable to resume printer %s - %s\n", PRINTERNAME(snum),
1522 ippErrorString(cupsLastError())));
1523 } else {
1524 ret = 0;
1526 } else {
1527 DEBUG(0,("Unable to resume printer %s - %s\n", PRINTERNAME(snum),
1528 ippErrorString(cupsLastError())));
1531 out:
1532 if (response)
1533 ippDelete(response);
1535 if (language)
1536 cupsLangFree(language);
1538 if (http)
1539 httpClose(http);
1541 TALLOC_FREE(frame);
1542 return ret;
1545 /*******************************************************************
1546 * CUPS printing interface definitions...
1547 ******************************************************************/
1549 struct printif cups_printif =
1551 PRINT_CUPS,
1552 cups_queue_get,
1553 cups_queue_pause,
1554 cups_queue_resume,
1555 cups_job_delete,
1556 cups_job_pause,
1557 cups_job_resume,
1558 cups_job_submit,
1561 bool cups_pull_comment_location(NT_PRINTER_INFO_LEVEL_2 *printer)
1563 TALLOC_CTX *frame = talloc_stackframe();
1564 http_t *http = NULL; /* HTTP connection to server */
1565 ipp_t *request = NULL, /* IPP Request */
1566 *response = NULL; /* IPP Response */
1567 ipp_attribute_t *attr; /* Current attribute */
1568 cups_lang_t *language = NULL; /* Default language */
1569 char uri[HTTP_MAX_URI];
1570 char *server = NULL;
1571 char *sharename = NULL;
1572 char *name = NULL;
1573 static const char *requested[] =/* Requested attributes */
1575 "printer-name",
1576 "printer-info",
1577 "printer-location"
1579 bool ret = False;
1581 DEBUG(5, ("pulling %s location\n", printer->sharename));
1584 * Make sure we don't ask for passwords...
1587 cupsSetPasswordCB(cups_passwd_cb);
1590 * Try to connect to the server...
1593 if ((http = cups_connect(frame)) == NULL) {
1594 goto out;
1597 request = ippNew();
1599 request->request.op.operation_id = IPP_GET_PRINTER_ATTRIBUTES;
1600 request->request.op.request_id = 1;
1602 language = cupsLangDefault();
1604 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1605 "attributes-charset", NULL, "utf-8");
1607 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1608 "attributes-natural-language", NULL, language->language);
1610 if (lp_cups_server() != NULL && strlen(lp_cups_server()) > 0) {
1611 if (push_utf8_talloc(frame, &server, lp_cups_server()) == (size_t)-1) {
1612 goto out;
1614 } else {
1615 server = talloc_strdup(frame,cupsServer());
1617 if (server) {
1618 goto out;
1620 if (push_utf8_talloc(frame, &sharename, printer->sharename) == (size_t)-1) {
1621 goto out;
1623 slprintf(uri, sizeof(uri) - 1, "ipp://%s/printers/%s",
1624 server, sharename);
1626 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1627 "printer-uri", NULL, uri);
1629 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1630 "requested-attributes",
1631 (sizeof(requested) / sizeof(requested[0])),
1632 NULL, requested);
1635 * Do the request and get back a response...
1638 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
1639 DEBUG(0,("Unable to get printer attributes - %s\n",
1640 ippErrorString(cupsLastError())));
1641 goto out;
1644 for (attr = response->attrs; attr != NULL;) {
1646 * Skip leading attributes until we hit a printer...
1649 while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)
1650 attr = attr->next;
1652 if (attr == NULL)
1653 break;
1656 * Pull the needed attributes from this printer...
1659 while ( attr && (attr->group_tag == IPP_TAG_PRINTER) ) {
1660 if (strcmp(attr->name, "printer-name") == 0 &&
1661 attr->value_tag == IPP_TAG_NAME) {
1662 pull_utf8_talloc(frame,
1663 &name,
1664 attr->values[0].string.text);
1667 /* Grab the comment if we don't have one */
1668 if ( (strcmp(attr->name, "printer-info") == 0)
1669 && (attr->value_tag == IPP_TAG_TEXT)
1670 && !strlen(printer->comment) )
1672 char *comment = NULL;
1673 pull_utf8_talloc(frame,
1674 &comment,
1675 attr->values[0].string.text);
1676 DEBUG(5,("cups_pull_comment_location: Using cups comment: %s\n",
1677 comment));
1678 strlcpy(printer->comment,
1679 comment,
1680 sizeof(printer->comment));
1683 /* Grab the location if we don't have one */
1684 if ( (strcmp(attr->name, "printer-location") == 0)
1685 && (attr->value_tag == IPP_TAG_TEXT)
1686 && !strlen(printer->location) )
1688 char *location = NULL;
1689 pull_utf8_talloc(frame,
1690 &location,
1691 attr->values[0].string.text);
1692 DEBUG(5,("cups_pull_comment_location: Using cups location: %s\n",
1693 location));
1694 strlcpy(printer->location,
1695 location,
1696 sizeof(printer->location));
1699 attr = attr->next;
1703 * We have everything needed...
1706 if (name != NULL)
1707 break;
1710 ret = True;
1712 out:
1713 if (response)
1714 ippDelete(response);
1716 if (request) {
1717 ippDelete(request);
1720 if (language)
1721 cupsLangFree(language);
1723 if (http)
1724 httpClose(http);
1726 TALLOC_FREE(frame);
1727 return ret;
1730 #else
1731 /* this keeps fussy compilers happy */
1732 void print_cups_dummy(void);
1733 void print_cups_dummy(void) {}
1734 #endif /* HAVE_CUPS */