Attempt to fix crash seen with new CUPS async printcap loading code.
[Samba.git] / source / printing / print_cups.c
blobf3eb73c40097ebfaf6b2bf7d56b86b0a11b6c02d
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(), true)) {
394 DEBUG(0,("cups_pcap_load_async: reinit_after_fork() failed\n"));
395 smb_panic("cups_pcap_load_async: reinit_after_fork() failed");
398 close(fds[0]);
399 cups_cache_reload_async(fds[1]);
400 close(fds[1]);
401 _exit(0);
404 static void cups_async_callback(struct event_context *event_ctx,
405 struct fd_event *event,
406 uint16 flags,
407 void *p)
409 TALLOC_CTX *frame = talloc_stackframe();
410 int fd = *(int *)p;
411 struct pcap_cache *tmp_pcap_cache = NULL;
413 DEBUG(5,("cups_async_callback: callback received for printer data. "
414 "fd = %d\n", fd));
416 while (1) {
417 char *name = NULL, *info = NULL;
418 size_t namelen = 0, infolen = 0;
419 ssize_t ret = -1;
421 ret = sys_read(fd, &namelen, sizeof(namelen));
422 if (ret == 0) {
423 /* EOF */
424 break;
426 if (ret != sizeof(namelen)) {
427 DEBUG(10,("cups_async_callback: namelen read failed %d %s\n",
428 errno, strerror(errno)));
429 break;
432 DEBUG(11,("cups_async_callback: read namelen %u\n",
433 (unsigned int)namelen));
435 ret = sys_read(fd, &infolen, sizeof(infolen));
436 if (ret == 0) {
437 /* EOF */
438 break;
440 if (ret != sizeof(infolen)) {
441 DEBUG(10,("cups_async_callback: infolen read failed %s\n",
442 strerror(errno)));
443 break;
446 DEBUG(11,("cups_async_callback: read infolen %u\n",
447 (unsigned int)infolen));
449 if (namelen) {
450 name = TALLOC_ARRAY(frame, char, namelen);
451 if (!name) {
452 break;
454 ret = sys_read(fd, name, namelen);
455 if (ret == 0) {
456 /* EOF */
457 break;
459 if (ret != namelen) {
460 DEBUG(10,("cups_async_callback: name read failed %s\n",
461 strerror(errno)));
462 break;
464 DEBUG(11,("cups_async_callback: read name %s\n",
465 name));
466 } else {
467 name = NULL;
469 if (infolen) {
470 info = TALLOC_ARRAY(frame, char, infolen);
471 if (!info) {
472 break;
474 ret = sys_read(fd, info, infolen);
475 if (ret == 0) {
476 /* EOF */
477 break;
479 if (ret != infolen) {
480 DEBUG(10,("cups_async_callback: info read failed %s\n",
481 strerror(errno)));
482 break;
484 DEBUG(11,("cups_async_callback: read info %s\n",
485 info));
486 } else {
487 info = NULL;
490 /* Add to our local pcap cache. */
491 pcap_cache_add_specific(&tmp_pcap_cache, name, info);
492 TALLOC_FREE(name);
493 TALLOC_FREE(info);
496 TALLOC_FREE(frame);
497 if (tmp_pcap_cache) {
498 /* We got a namelist, replace our local cache. */
499 pcap_cache_destroy_specific(&local_pcap_copy);
500 local_pcap_copy = tmp_pcap_cache;
502 /* And the systemwide pcap cache. */
503 pcap_cache_replace(local_pcap_copy);
504 } else {
505 DEBUG(2,("cups_async_callback: failed to read a new "
506 "printer list\n"));
508 close(fd);
509 TALLOC_FREE(p);
510 TALLOC_FREE(cache_fd_event);
513 bool cups_cache_reload(void)
515 int *p_pipe_fd = TALLOC_P(NULL, int);
517 if (!p_pipe_fd) {
518 return false;
521 *p_pipe_fd = -1;
523 /* Set up an async refresh. */
524 if (!cups_pcap_load_async(p_pipe_fd)) {
525 return false;
527 if (!local_pcap_copy) {
528 /* We have no local cache, wait directly for
529 * async refresh to complete.
531 DEBUG(10,("cups_cache_reload: sync read on fd %d\n",
532 *p_pipe_fd ));
534 cups_async_callback(smbd_event_context(),
535 NULL,
536 EVENT_FD_READ,
537 (void *)p_pipe_fd);
538 if (!local_pcap_copy) {
539 return false;
541 } else {
542 /* Replace the system cache with our
543 * local copy. */
544 pcap_cache_replace(local_pcap_copy);
546 DEBUG(10,("cups_cache_reload: async read on fd %d\n",
547 *p_pipe_fd ));
549 /* Trigger an event when the pipe can be read. */
550 cache_fd_event = event_add_fd(smbd_event_context(),
551 NULL, *p_pipe_fd,
552 EVENT_FD_READ,
553 cups_async_callback,
554 (void *)p_pipe_fd);
555 if (!cache_fd_event) {
556 close(*p_pipe_fd);
557 TALLOC_FREE(p_pipe_fd);
558 return false;
561 return true;
565 * 'cups_job_delete()' - Delete a job.
568 static int cups_job_delete(const char *sharename, const char *lprm_command, struct printjob *pjob)
570 TALLOC_CTX *frame = talloc_stackframe();
571 int ret = 1; /* Return value */
572 http_t *http = NULL; /* HTTP connection to server */
573 ipp_t *request = NULL, /* IPP Request */
574 *response = NULL; /* IPP Response */
575 cups_lang_t *language = NULL; /* Default language */
576 char *user = NULL;
577 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
580 DEBUG(5,("cups_job_delete(%s, %p (%d))\n", sharename, pjob, pjob->sysjob));
583 * Make sure we don't ask for passwords...
586 cupsSetPasswordCB(cups_passwd_cb);
589 * Try to connect to the server...
592 if ((http = cups_connect(frame)) == NULL) {
593 goto out;
597 * Build an IPP_CANCEL_JOB request, which requires the following
598 * attributes:
600 * attributes-charset
601 * attributes-natural-language
602 * job-uri
603 * requesting-user-name
606 request = ippNew();
608 request->request.op.operation_id = IPP_CANCEL_JOB;
609 request->request.op.request_id = 1;
611 language = cupsLangDefault();
613 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
614 "attributes-charset", NULL, "utf-8");
616 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
617 "attributes-natural-language", NULL, language->language);
619 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/jobs/%d", pjob->sysjob);
621 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri);
623 if (push_utf8_talloc(frame, &user, pjob->user) == (size_t)-1) {
624 goto out;
627 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
628 NULL, user);
631 * Do the request and get back a response...
634 if ((response = cupsDoRequest(http, request, "/jobs")) != NULL) {
635 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
636 DEBUG(0,("Unable to cancel job %d - %s\n", pjob->sysjob,
637 ippErrorString(cupsLastError())));
638 } else {
639 ret = 0;
641 } else {
642 DEBUG(0,("Unable to cancel job %d - %s\n", pjob->sysjob,
643 ippErrorString(cupsLastError())));
646 out:
647 if (response)
648 ippDelete(response);
650 if (language)
651 cupsLangFree(language);
653 if (http)
654 httpClose(http);
656 TALLOC_FREE(frame);
657 return ret;
662 * 'cups_job_pause()' - Pause a job.
665 static int cups_job_pause(int snum, struct printjob *pjob)
667 TALLOC_CTX *frame = talloc_stackframe();
668 int ret = 1; /* Return value */
669 http_t *http = NULL; /* HTTP connection to server */
670 ipp_t *request = NULL, /* IPP Request */
671 *response = NULL; /* IPP Response */
672 cups_lang_t *language = NULL; /* Default language */
673 char *user = NULL;
674 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
677 DEBUG(5,("cups_job_pause(%d, %p (%d))\n", snum, pjob, pjob->sysjob));
680 * Make sure we don't ask for passwords...
683 cupsSetPasswordCB(cups_passwd_cb);
686 * Try to connect to the server...
689 if ((http = cups_connect(frame)) == NULL) {
690 goto out;
694 * Build an IPP_HOLD_JOB request, which requires the following
695 * attributes:
697 * attributes-charset
698 * attributes-natural-language
699 * job-uri
700 * requesting-user-name
703 request = ippNew();
705 request->request.op.operation_id = IPP_HOLD_JOB;
706 request->request.op.request_id = 1;
708 language = cupsLangDefault();
710 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
711 "attributes-charset", NULL, "utf-8");
713 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
714 "attributes-natural-language", NULL, language->language);
716 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/jobs/%d", pjob->sysjob);
718 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri);
720 if (push_utf8_talloc(frame, &user, pjob->user) == (size_t)-1) {
721 goto out;
723 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
724 NULL, user);
727 * Do the request and get back a response...
730 if ((response = cupsDoRequest(http, request, "/jobs")) != NULL) {
731 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
732 DEBUG(0,("Unable to hold job %d - %s\n", pjob->sysjob,
733 ippErrorString(cupsLastError())));
734 } else {
735 ret = 0;
737 } else {
738 DEBUG(0,("Unable to hold job %d - %s\n", pjob->sysjob,
739 ippErrorString(cupsLastError())));
742 out:
743 if (response)
744 ippDelete(response);
746 if (language)
747 cupsLangFree(language);
749 if (http)
750 httpClose(http);
752 TALLOC_FREE(frame);
753 return ret;
758 * 'cups_job_resume()' - Resume a paused job.
761 static int cups_job_resume(int snum, struct printjob *pjob)
763 TALLOC_CTX *frame = talloc_stackframe();
764 int ret = 1; /* Return value */
765 http_t *http = NULL; /* HTTP connection to server */
766 ipp_t *request = NULL, /* IPP Request */
767 *response = NULL; /* IPP Response */
768 cups_lang_t *language = NULL; /* Default language */
769 char *user = NULL;
770 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
773 DEBUG(5,("cups_job_resume(%d, %p (%d))\n", snum, pjob, pjob->sysjob));
776 * Make sure we don't ask for passwords...
779 cupsSetPasswordCB(cups_passwd_cb);
782 * Try to connect to the server...
785 if ((http = cups_connect(frame)) == NULL) {
786 goto out;
790 * Build an IPP_RELEASE_JOB request, which requires the following
791 * attributes:
793 * attributes-charset
794 * attributes-natural-language
795 * job-uri
796 * requesting-user-name
799 request = ippNew();
801 request->request.op.operation_id = IPP_RELEASE_JOB;
802 request->request.op.request_id = 1;
804 language = cupsLangDefault();
806 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
807 "attributes-charset", NULL, "utf-8");
809 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
810 "attributes-natural-language", NULL, language->language);
812 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/jobs/%d", pjob->sysjob);
814 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri);
816 if (push_utf8_talloc(frame, &user, pjob->user) == (size_t)-1) {
817 goto out;
819 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
820 NULL, user);
823 * Do the request and get back a response...
826 if ((response = cupsDoRequest(http, request, "/jobs")) != NULL) {
827 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
828 DEBUG(0,("Unable to release job %d - %s\n", pjob->sysjob,
829 ippErrorString(cupsLastError())));
830 } else {
831 ret = 0;
833 } else {
834 DEBUG(0,("Unable to release job %d - %s\n", pjob->sysjob,
835 ippErrorString(cupsLastError())));
838 out:
839 if (response)
840 ippDelete(response);
842 if (language)
843 cupsLangFree(language);
845 if (http)
846 httpClose(http);
848 TALLOC_FREE(frame);
849 return ret;
854 * 'cups_job_submit()' - Submit a job for printing.
857 static int cups_job_submit(int snum, struct printjob *pjob)
859 TALLOC_CTX *frame = talloc_stackframe();
860 int ret = 1; /* Return value */
861 http_t *http = NULL; /* HTTP connection to server */
862 ipp_t *request = NULL, /* IPP Request */
863 *response = NULL; /* IPP Response */
864 cups_lang_t *language = NULL; /* Default language */
865 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
866 const char *clientname = NULL; /* hostname of client for job-originating-host attribute */
867 char *new_jobname = NULL;
868 int num_options = 0;
869 cups_option_t *options = NULL;
870 char *printername = NULL;
871 char *user = NULL;
872 char *jobname = NULL;
873 char *cupsoptions = NULL;
874 char *filename = NULL;
875 char addr[INET6_ADDRSTRLEN];
877 DEBUG(5,("cups_job_submit(%d, %p (%d))\n", snum, pjob, pjob->sysjob));
880 * Make sure we don't ask for passwords...
883 cupsSetPasswordCB(cups_passwd_cb);
886 * Try to connect to the server...
889 if ((http = cups_connect(frame)) == NULL) {
890 goto out;
894 * Build an IPP_PRINT_JOB request, which requires the following
895 * attributes:
897 * attributes-charset
898 * attributes-natural-language
899 * printer-uri
900 * requesting-user-name
901 * [document-data]
904 request = ippNew();
906 request->request.op.operation_id = IPP_PRINT_JOB;
907 request->request.op.request_id = 1;
909 language = cupsLangDefault();
911 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
912 "attributes-charset", NULL, "utf-8");
914 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
915 "attributes-natural-language", NULL, language->language);
917 if (push_utf8_talloc(frame, &printername, PRINTERNAME(snum)) == (size_t)-1) {
918 goto out;
920 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s",
921 printername);
923 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
924 "printer-uri", NULL, uri);
926 if (push_utf8_talloc(frame, &user, pjob->user) == (size_t)-1) {
927 goto out;
929 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
930 NULL, user);
932 clientname = client_name(get_client_fd());
933 if (strcmp(clientname, "UNKNOWN") == 0) {
934 clientname = client_addr(get_client_fd(),addr,sizeof(addr));
937 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
938 "job-originating-host-name", NULL,
939 clientname);
941 if (push_utf8_talloc(frame, &jobname, pjob->jobname) == (size_t)-1) {
942 goto out;
944 new_jobname = talloc_asprintf(frame,
945 "%s%.8u %s", PRINT_SPOOL_PREFIX,
946 (unsigned int)pjob->smbjob,
947 jobname);
948 if (new_jobname == NULL) {
949 goto out;
952 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name", NULL,
953 new_jobname);
956 * add any options defined in smb.conf
959 if (push_utf8_talloc(frame, &cupsoptions, lp_cups_options(snum)) == (size_t)-1) {
960 goto out;
962 num_options = 0;
963 options = NULL;
964 num_options = cupsParseOptions(cupsoptions, num_options, &options);
966 if ( num_options )
967 cupsEncodeOptions(request, num_options, options);
970 * Do the request and get back a response...
973 slprintf(uri, sizeof(uri) - 1, "/printers/%s", printername);
975 if (push_utf8_talloc(frame, &filename, pjob->filename) == (size_t)-1) {
976 goto out;
978 if ((response = cupsDoFileRequest(http, request, uri, pjob->filename)) != NULL) {
979 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
980 DEBUG(0,("Unable to print file to %s - %s\n", PRINTERNAME(snum),
981 ippErrorString(cupsLastError())));
982 } else {
983 ret = 0;
985 } else {
986 DEBUG(0,("Unable to print file to `%s' - %s\n", PRINTERNAME(snum),
987 ippErrorString(cupsLastError())));
990 if ( ret == 0 )
991 unlink(pjob->filename);
992 /* else print_job_end will do it for us */
994 out:
995 if (response)
996 ippDelete(response);
998 if (language)
999 cupsLangFree(language);
1001 if (http)
1002 httpClose(http);
1004 TALLOC_FREE(frame);
1006 return ret;
1010 * 'cups_queue_get()' - Get all the jobs in the print queue.
1013 static int cups_queue_get(const char *sharename,
1014 enum printing_types printing_type,
1015 char *lpq_command,
1016 print_queue_struct **q,
1017 print_status_struct *status)
1019 TALLOC_CTX *frame = talloc_stackframe();
1020 char *printername = NULL;
1021 http_t *http = NULL; /* HTTP connection to server */
1022 ipp_t *request = NULL, /* IPP Request */
1023 *response = NULL; /* IPP Response */
1024 ipp_attribute_t *attr = NULL; /* Current attribute */
1025 cups_lang_t *language = NULL; /* Default language */
1026 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
1027 int qcount = 0, /* Number of active queue entries */
1028 qalloc = 0; /* Number of queue entries allocated */
1029 print_queue_struct *queue = NULL, /* Queue entries */
1030 *temp; /* Temporary pointer for queue */
1031 char *user_name = NULL, /* job-originating-user-name attribute */
1032 *job_name = NULL; /* job-name attribute */
1033 int job_id; /* job-id attribute */
1034 int job_k_octets; /* job-k-octets attribute */
1035 time_t job_time; /* time-at-creation attribute */
1036 ipp_jstate_t job_status; /* job-status attribute */
1037 int job_priority; /* job-priority attribute */
1038 static const char *jattrs[] = /* Requested job attributes */
1040 "job-id",
1041 "job-k-octets",
1042 "job-name",
1043 "job-originating-user-name",
1044 "job-priority",
1045 "job-state",
1046 "time-at-creation",
1048 static const char *pattrs[] = /* Requested printer attributes */
1050 "printer-state",
1051 "printer-state-message"
1054 *q = NULL;
1056 /* HACK ALERT!!! The problem with support the 'printer name'
1057 option is that we key the tdb off the sharename. So we will
1058 overload the lpq_command string to pass in the printername
1059 (which is basically what we do for non-cups printers ... using
1060 the lpq_command to get the queue listing). */
1062 if (push_utf8_talloc(frame, &printername, lpq_command) == (size_t)-1) {
1063 goto out;
1065 DEBUG(5,("cups_queue_get(%s, %p, %p)\n", lpq_command, q, status));
1068 * Make sure we don't ask for passwords...
1071 cupsSetPasswordCB(cups_passwd_cb);
1074 * Try to connect to the server...
1077 if ((http = cups_connect(frame)) == NULL) {
1078 goto out;
1082 * Generate the printer URI...
1085 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s", printername);
1088 * Build an IPP_GET_JOBS request, which requires the following
1089 * attributes:
1091 * attributes-charset
1092 * attributes-natural-language
1093 * requested-attributes
1094 * printer-uri
1097 request = ippNew();
1099 request->request.op.operation_id = IPP_GET_JOBS;
1100 request->request.op.request_id = 1;
1102 language = cupsLangDefault();
1104 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1105 "attributes-charset", NULL, "utf-8");
1107 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1108 "attributes-natural-language", NULL, language->language);
1110 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1111 "requested-attributes",
1112 (sizeof(jattrs) / sizeof(jattrs[0])),
1113 NULL, jattrs);
1115 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1116 "printer-uri", NULL, uri);
1119 * Do the request and get back a response...
1122 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
1123 DEBUG(0,("Unable to get jobs for %s - %s\n", uri,
1124 ippErrorString(cupsLastError())));
1125 goto out;
1128 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1129 DEBUG(0,("Unable to get jobs for %s - %s\n", uri,
1130 ippErrorString(response->request.status.status_code)));
1131 goto out;
1135 * Process the jobs...
1138 qcount = 0;
1139 qalloc = 0;
1140 queue = NULL;
1142 for (attr = response->attrs; attr != NULL; attr = attr->next) {
1144 * Skip leading attributes until we hit a job...
1147 while (attr != NULL && attr->group_tag != IPP_TAG_JOB)
1148 attr = attr->next;
1150 if (attr == NULL)
1151 break;
1154 * Allocate memory as needed...
1156 if (qcount >= qalloc) {
1157 qalloc += 16;
1159 queue = SMB_REALLOC_ARRAY(queue, print_queue_struct, qalloc);
1161 if (queue == NULL) {
1162 DEBUG(0,("cups_queue_get: Not enough memory!"));
1163 qcount = 0;
1164 goto out;
1168 temp = queue + qcount;
1169 memset(temp, 0, sizeof(print_queue_struct));
1172 * Pull the needed attributes from this job...
1175 job_id = 0;
1176 job_priority = 50;
1177 job_status = IPP_JOB_PENDING;
1178 job_time = 0;
1179 job_k_octets = 0;
1180 user_name = NULL;
1181 job_name = NULL;
1183 while (attr != NULL && attr->group_tag == IPP_TAG_JOB) {
1184 if (attr->name == NULL) {
1185 attr = attr->next;
1186 break;
1189 if (strcmp(attr->name, "job-id") == 0 &&
1190 attr->value_tag == IPP_TAG_INTEGER)
1191 job_id = attr->values[0].integer;
1193 if (strcmp(attr->name, "job-k-octets") == 0 &&
1194 attr->value_tag == IPP_TAG_INTEGER)
1195 job_k_octets = attr->values[0].integer;
1197 if (strcmp(attr->name, "job-priority") == 0 &&
1198 attr->value_tag == IPP_TAG_INTEGER)
1199 job_priority = attr->values[0].integer;
1201 if (strcmp(attr->name, "job-state") == 0 &&
1202 attr->value_tag == IPP_TAG_ENUM)
1203 job_status = (ipp_jstate_t)(attr->values[0].integer);
1205 if (strcmp(attr->name, "time-at-creation") == 0 &&
1206 attr->value_tag == IPP_TAG_INTEGER)
1207 job_time = attr->values[0].integer;
1209 if (strcmp(attr->name, "job-name") == 0 &&
1210 attr->value_tag == IPP_TAG_NAME) {
1211 pull_utf8_talloc(frame,
1212 &job_name,
1213 attr->values[0].string.text);
1216 if (strcmp(attr->name, "job-originating-user-name") == 0 &&
1217 attr->value_tag == IPP_TAG_NAME) {
1218 pull_utf8_talloc(frame,
1219 &user_name,
1220 attr->values[0].string.text);
1223 attr = attr->next;
1227 * See if we have everything needed...
1230 if (user_name == NULL || job_name == NULL || job_id == 0) {
1231 if (attr == NULL)
1232 break;
1233 else
1234 continue;
1237 temp->job = job_id;
1238 temp->size = job_k_octets * 1024;
1239 temp->status = job_status == IPP_JOB_PENDING ? LPQ_QUEUED :
1240 job_status == IPP_JOB_STOPPED ? LPQ_PAUSED :
1241 job_status == IPP_JOB_HELD ? LPQ_PAUSED :
1242 LPQ_PRINTING;
1243 temp->priority = job_priority;
1244 temp->time = job_time;
1245 strlcpy(temp->fs_user, user_name, sizeof(temp->fs_user));
1246 strlcpy(temp->fs_file, job_name, sizeof(temp->fs_file));
1248 qcount ++;
1250 if (attr == NULL)
1251 break;
1254 ippDelete(response);
1255 response = NULL;
1258 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the
1259 * following attributes:
1261 * attributes-charset
1262 * attributes-natural-language
1263 * requested-attributes
1264 * printer-uri
1267 request = ippNew();
1269 request->request.op.operation_id = IPP_GET_PRINTER_ATTRIBUTES;
1270 request->request.op.request_id = 1;
1272 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1273 "attributes-charset", NULL, "utf-8");
1275 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1276 "attributes-natural-language", NULL, language->language);
1278 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1279 "requested-attributes",
1280 (sizeof(pattrs) / sizeof(pattrs[0])),
1281 NULL, pattrs);
1283 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1284 "printer-uri", NULL, uri);
1287 * Do the request and get back a response...
1290 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
1291 DEBUG(0,("Unable to get printer status for %s - %s\n", printername,
1292 ippErrorString(cupsLastError())));
1293 *q = queue;
1294 goto out;
1297 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1298 DEBUG(0,("Unable to get printer status for %s - %s\n", printername,
1299 ippErrorString(response->request.status.status_code)));
1300 *q = queue;
1301 goto out;
1305 * Get the current printer status and convert it to the SAMBA values.
1308 if ((attr = ippFindAttribute(response, "printer-state", IPP_TAG_ENUM)) != NULL) {
1309 if (attr->values[0].integer == IPP_PRINTER_STOPPED)
1310 status->status = LPSTAT_STOPPED;
1311 else
1312 status->status = LPSTAT_OK;
1315 if ((attr = ippFindAttribute(response, "printer-state-message",
1316 IPP_TAG_TEXT)) != NULL) {
1317 char *msg = NULL;
1318 pull_utf8_talloc(frame, &msg, attr->values[0].string.text);
1319 fstrcpy(status->message, msg);
1323 * Return the job queue...
1326 *q = queue;
1328 out:
1329 if (response)
1330 ippDelete(response);
1332 if (language)
1333 cupsLangFree(language);
1335 if (http)
1336 httpClose(http);
1338 TALLOC_FREE(frame);
1339 return qcount;
1344 * 'cups_queue_pause()' - Pause a print queue.
1347 static int cups_queue_pause(int snum)
1349 TALLOC_CTX *frame = talloc_stackframe();
1350 int ret = 1; /* Return value */
1351 http_t *http = NULL; /* HTTP connection to server */
1352 ipp_t *request = NULL, /* IPP Request */
1353 *response = NULL; /* IPP Response */
1354 cups_lang_t *language = NULL; /* Default language */
1355 char *printername = NULL;
1356 char *username = NULL;
1357 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
1360 DEBUG(5,("cups_queue_pause(%d)\n", snum));
1363 * Make sure we don't ask for passwords...
1366 cupsSetPasswordCB(cups_passwd_cb);
1369 * Try to connect to the server...
1372 if ((http = cups_connect(frame)) == NULL) {
1373 goto out;
1377 * Build an IPP_PAUSE_PRINTER request, which requires the following
1378 * attributes:
1380 * attributes-charset
1381 * attributes-natural-language
1382 * printer-uri
1383 * requesting-user-name
1386 request = ippNew();
1388 request->request.op.operation_id = IPP_PAUSE_PRINTER;
1389 request->request.op.request_id = 1;
1391 language = cupsLangDefault();
1393 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1394 "attributes-charset", NULL, "utf-8");
1396 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1397 "attributes-natural-language", NULL, language->language);
1399 if (push_utf8_talloc(frame, &printername, PRINTERNAME(snum)) == (size_t)-1) {
1400 goto out;
1402 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s",
1403 printername);
1405 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
1407 if (push_utf8_talloc(frame, &username, current_user_info.unix_name) == (size_t)-1) {
1408 goto out;
1410 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
1411 NULL, username);
1414 * Do the request and get back a response...
1417 if ((response = cupsDoRequest(http, request, "/admin/")) != NULL) {
1418 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1419 DEBUG(0,("Unable to pause printer %s - %s\n", PRINTERNAME(snum),
1420 ippErrorString(cupsLastError())));
1421 } else {
1422 ret = 0;
1424 } else {
1425 DEBUG(0,("Unable to pause printer %s - %s\n", PRINTERNAME(snum),
1426 ippErrorString(cupsLastError())));
1429 out:
1430 if (response)
1431 ippDelete(response);
1433 if (language)
1434 cupsLangFree(language);
1436 if (http)
1437 httpClose(http);
1439 TALLOC_FREE(frame);
1440 return ret;
1445 * 'cups_queue_resume()' - Restart a print queue.
1448 static int cups_queue_resume(int snum)
1450 TALLOC_CTX *frame = talloc_stackframe();
1451 int ret = 1; /* Return value */
1452 http_t *http = NULL; /* HTTP connection to server */
1453 ipp_t *request = NULL, /* IPP Request */
1454 *response = NULL; /* IPP Response */
1455 cups_lang_t *language = NULL; /* Default language */
1456 char *printername = NULL;
1457 char *username = NULL;
1458 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
1461 DEBUG(5,("cups_queue_resume(%d)\n", snum));
1464 * Make sure we don't ask for passwords...
1467 cupsSetPasswordCB(cups_passwd_cb);
1470 * Try to connect to the server...
1473 if ((http = cups_connect(frame)) == NULL) {
1474 goto out;
1478 * Build an IPP_RESUME_PRINTER request, which requires the following
1479 * attributes:
1481 * attributes-charset
1482 * attributes-natural-language
1483 * printer-uri
1484 * requesting-user-name
1487 request = ippNew();
1489 request->request.op.operation_id = IPP_RESUME_PRINTER;
1490 request->request.op.request_id = 1;
1492 language = cupsLangDefault();
1494 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1495 "attributes-charset", NULL, "utf-8");
1497 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1498 "attributes-natural-language", NULL, language->language);
1500 if (push_utf8_talloc(frame, &printername, PRINTERNAME(snum)) == (size_t)-1) {
1501 goto out;
1503 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s",
1504 printername);
1506 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
1508 if (push_utf8_talloc(frame, &username, current_user_info.unix_name) == (size_t)-1) {
1509 goto out;
1511 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
1512 NULL, username);
1515 * Do the request and get back a response...
1518 if ((response = cupsDoRequest(http, request, "/admin/")) != NULL) {
1519 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1520 DEBUG(0,("Unable to resume printer %s - %s\n", PRINTERNAME(snum),
1521 ippErrorString(cupsLastError())));
1522 } else {
1523 ret = 0;
1525 } else {
1526 DEBUG(0,("Unable to resume printer %s - %s\n", PRINTERNAME(snum),
1527 ippErrorString(cupsLastError())));
1530 out:
1531 if (response)
1532 ippDelete(response);
1534 if (language)
1535 cupsLangFree(language);
1537 if (http)
1538 httpClose(http);
1540 TALLOC_FREE(frame);
1541 return ret;
1544 /*******************************************************************
1545 * CUPS printing interface definitions...
1546 ******************************************************************/
1548 struct printif cups_printif =
1550 PRINT_CUPS,
1551 cups_queue_get,
1552 cups_queue_pause,
1553 cups_queue_resume,
1554 cups_job_delete,
1555 cups_job_pause,
1556 cups_job_resume,
1557 cups_job_submit,
1560 bool cups_pull_comment_location(NT_PRINTER_INFO_LEVEL_2 *printer)
1562 TALLOC_CTX *frame = talloc_stackframe();
1563 http_t *http = NULL; /* HTTP connection to server */
1564 ipp_t *request = NULL, /* IPP Request */
1565 *response = NULL; /* IPP Response */
1566 ipp_attribute_t *attr; /* Current attribute */
1567 cups_lang_t *language = NULL; /* Default language */
1568 char uri[HTTP_MAX_URI];
1569 char *server = NULL;
1570 char *sharename = NULL;
1571 char *name = NULL;
1572 static const char *requested[] =/* Requested attributes */
1574 "printer-name",
1575 "printer-info",
1576 "printer-location"
1578 bool ret = False;
1580 DEBUG(5, ("pulling %s location\n", printer->sharename));
1583 * Make sure we don't ask for passwords...
1586 cupsSetPasswordCB(cups_passwd_cb);
1589 * Try to connect to the server...
1592 if ((http = cups_connect(frame)) == NULL) {
1593 goto out;
1596 request = ippNew();
1598 request->request.op.operation_id = IPP_GET_PRINTER_ATTRIBUTES;
1599 request->request.op.request_id = 1;
1601 language = cupsLangDefault();
1603 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1604 "attributes-charset", NULL, "utf-8");
1606 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1607 "attributes-natural-language", NULL, language->language);
1609 if (lp_cups_server() != NULL && strlen(lp_cups_server()) > 0) {
1610 if (push_utf8_talloc(frame, &server, lp_cups_server()) == (size_t)-1) {
1611 goto out;
1613 } else {
1614 server = talloc_strdup(frame,cupsServer());
1616 if (server) {
1617 goto out;
1619 if (push_utf8_talloc(frame, &sharename, printer->sharename) == (size_t)-1) {
1620 goto out;
1622 slprintf(uri, sizeof(uri) - 1, "ipp://%s/printers/%s",
1623 server, sharename);
1625 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1626 "printer-uri", NULL, uri);
1628 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1629 "requested-attributes",
1630 (sizeof(requested) / sizeof(requested[0])),
1631 NULL, requested);
1634 * Do the request and get back a response...
1637 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
1638 DEBUG(0,("Unable to get printer attributes - %s\n",
1639 ippErrorString(cupsLastError())));
1640 goto out;
1643 for (attr = response->attrs; attr != NULL;) {
1645 * Skip leading attributes until we hit a printer...
1648 while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)
1649 attr = attr->next;
1651 if (attr == NULL)
1652 break;
1655 * Pull the needed attributes from this printer...
1658 while ( attr && (attr->group_tag == IPP_TAG_PRINTER) ) {
1659 if (strcmp(attr->name, "printer-name") == 0 &&
1660 attr->value_tag == IPP_TAG_NAME) {
1661 pull_utf8_talloc(frame,
1662 &name,
1663 attr->values[0].string.text);
1666 /* Grab the comment if we don't have one */
1667 if ( (strcmp(attr->name, "printer-info") == 0)
1668 && (attr->value_tag == IPP_TAG_TEXT)
1669 && !strlen(printer->comment) )
1671 char *comment = NULL;
1672 pull_utf8_talloc(frame,
1673 &comment,
1674 attr->values[0].string.text);
1675 DEBUG(5,("cups_pull_comment_location: Using cups comment: %s\n",
1676 comment));
1677 strlcpy(printer->comment,
1678 comment,
1679 sizeof(printer->comment));
1682 /* Grab the location if we don't have one */
1683 if ( (strcmp(attr->name, "printer-location") == 0)
1684 && (attr->value_tag == IPP_TAG_TEXT)
1685 && !strlen(printer->location) )
1687 char *location = NULL;
1688 pull_utf8_talloc(frame,
1689 &location,
1690 attr->values[0].string.text);
1691 DEBUG(5,("cups_pull_comment_location: Using cups location: %s\n",
1692 location));
1693 strlcpy(printer->location,
1694 location,
1695 sizeof(printer->location));
1698 attr = attr->next;
1702 * We have everything needed...
1705 if (name != NULL)
1706 break;
1709 ret = True;
1711 out:
1712 if (response)
1713 ippDelete(response);
1715 if (request) {
1716 ippDelete(request);
1719 if (language)
1720 cupsLangFree(language);
1722 if (http)
1723 httpClose(http);
1725 TALLOC_FREE(frame);
1726 return ret;
1729 #else
1730 /* this keeps fussy compilers happy */
1731 void print_cups_dummy(void);
1732 void print_cups_dummy(void) {}
1733 #endif /* HAVE_CUPS */