s3:smbd: close the low level fd of the base_fsp, if the file was created
[Samba.git] / source / printing / print_cups.c
blob8d32ddbc71e0394848a96310f81c16f8ff5a5521
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 if (!reinit_after_fork(smbd_messaging_context(), true)) {
392 DEBUG(0,("cups_pcap_load_async: reinit_after_fork() failed\n"));
393 smb_panic("cups_pcap_load_async: reinit_after_fork() failed");
396 close(fds[0]);
397 cups_cache_reload_async(fds[1]);
398 close(fds[1]);
399 _exit(0);
402 static void cups_async_callback(struct event_context *event_ctx,
403 struct fd_event *event,
404 uint16 flags,
405 void *p)
407 TALLOC_CTX *frame = talloc_stackframe();
408 int fd = *(int *)p;
409 struct pcap_cache *tmp_pcap_cache = NULL;
411 DEBUG(5,("cups_async_callback: callback received for printer data. "
412 "fd = %d\n", fd));
414 while (1) {
415 char *name = NULL, *info = NULL;
416 size_t namelen = 0, infolen = 0;
417 ssize_t ret = -1;
419 ret = sys_read(fd, &namelen, sizeof(namelen));
420 if (ret == 0) {
421 /* EOF */
422 break;
424 if (ret != sizeof(namelen)) {
425 DEBUG(10,("cups_async_callback: namelen read failed %d %s\n",
426 errno, strerror(errno)));
427 break;
430 DEBUG(11,("cups_async_callback: read namelen %u\n",
431 (unsigned int)namelen));
433 ret = sys_read(fd, &infolen, sizeof(infolen));
434 if (ret == 0) {
435 /* EOF */
436 break;
438 if (ret != sizeof(infolen)) {
439 DEBUG(10,("cups_async_callback: infolen read failed %s\n",
440 strerror(errno)));
441 break;
444 DEBUG(11,("cups_async_callback: read infolen %u\n",
445 (unsigned int)infolen));
447 if (namelen) {
448 name = TALLOC_ARRAY(frame, char, namelen);
449 if (!name) {
450 break;
452 ret = sys_read(fd, name, namelen);
453 if (ret == 0) {
454 /* EOF */
455 break;
457 if (ret != namelen) {
458 DEBUG(10,("cups_async_callback: name read failed %s\n",
459 strerror(errno)));
460 break;
462 DEBUG(11,("cups_async_callback: read name %s\n",
463 name));
464 } else {
465 name = NULL;
467 if (infolen) {
468 info = TALLOC_ARRAY(frame, char, infolen);
469 if (!info) {
470 break;
472 ret = sys_read(fd, info, infolen);
473 if (ret == 0) {
474 /* EOF */
475 break;
477 if (ret != infolen) {
478 DEBUG(10,("cups_async_callback: info read failed %s\n",
479 strerror(errno)));
480 break;
482 DEBUG(11,("cups_async_callback: read info %s\n",
483 info));
484 } else {
485 info = NULL;
488 /* Add to our local pcap cache. */
489 pcap_cache_add_specific(&tmp_pcap_cache, name, info);
490 TALLOC_FREE(name);
491 TALLOC_FREE(info);
494 TALLOC_FREE(frame);
495 if (tmp_pcap_cache) {
496 /* We got a namelist, replace our local cache. */
497 pcap_cache_destroy_specific(&local_pcap_copy);
498 local_pcap_copy = tmp_pcap_cache;
500 /* And the systemwide pcap cache. */
501 pcap_cache_replace(local_pcap_copy);
502 } else {
503 DEBUG(2,("cups_async_callback: failed to read a new "
504 "printer list\n"));
506 close(fd);
507 TALLOC_FREE(p);
508 TALLOC_FREE(cache_fd_event);
511 bool cups_cache_reload(void)
513 int *p_pipe_fd = TALLOC_P(NULL, int);
515 if (!p_pipe_fd) {
516 return false;
519 *p_pipe_fd = -1;
521 /* Set up an async refresh. */
522 if (!cups_pcap_load_async(p_pipe_fd)) {
523 return false;
525 if (!local_pcap_copy) {
526 /* We have no local cache, wait directly for
527 * async refresh to complete.
529 DEBUG(10,("cups_cache_reload: sync read on fd %d\n",
530 *p_pipe_fd ));
532 cups_async_callback(smbd_event_context(),
533 NULL,
534 EVENT_FD_READ,
535 (void *)p_pipe_fd);
536 if (!local_pcap_copy) {
537 return false;
539 } else {
540 /* Replace the system cache with our
541 * local copy. */
542 pcap_cache_replace(local_pcap_copy);
544 DEBUG(10,("cups_cache_reload: async read on fd %d\n",
545 *p_pipe_fd ));
547 /* Trigger an event when the pipe can be read. */
548 cache_fd_event = event_add_fd(smbd_event_context(),
549 NULL, *p_pipe_fd,
550 EVENT_FD_READ,
551 cups_async_callback,
552 (void *)p_pipe_fd);
553 if (!cache_fd_event) {
554 close(*p_pipe_fd);
555 TALLOC_FREE(p_pipe_fd);
556 return false;
559 return true;
563 * 'cups_job_delete()' - Delete a job.
566 static int cups_job_delete(const char *sharename, const char *lprm_command, struct printjob *pjob)
568 TALLOC_CTX *frame = talloc_stackframe();
569 int ret = 1; /* Return value */
570 http_t *http = NULL; /* HTTP connection to server */
571 ipp_t *request = NULL, /* IPP Request */
572 *response = NULL; /* IPP Response */
573 cups_lang_t *language = NULL; /* Default language */
574 char *user = NULL;
575 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
578 DEBUG(5,("cups_job_delete(%s, %p (%d))\n", sharename, pjob, pjob->sysjob));
581 * Make sure we don't ask for passwords...
584 cupsSetPasswordCB(cups_passwd_cb);
587 * Try to connect to the server...
590 if ((http = cups_connect(frame)) == NULL) {
591 goto out;
595 * Build an IPP_CANCEL_JOB request, which requires the following
596 * attributes:
598 * attributes-charset
599 * attributes-natural-language
600 * job-uri
601 * requesting-user-name
604 request = ippNew();
606 request->request.op.operation_id = IPP_CANCEL_JOB;
607 request->request.op.request_id = 1;
609 language = cupsLangDefault();
611 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
612 "attributes-charset", NULL, "utf-8");
614 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
615 "attributes-natural-language", NULL, language->language);
617 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/jobs/%d", pjob->sysjob);
619 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri);
621 if (push_utf8_talloc(frame, &user, pjob->user) == (size_t)-1) {
622 goto out;
625 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
626 NULL, user);
629 * Do the request and get back a response...
632 if ((response = cupsDoRequest(http, request, "/jobs")) != NULL) {
633 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
634 DEBUG(0,("Unable to cancel job %d - %s\n", pjob->sysjob,
635 ippErrorString(cupsLastError())));
636 } else {
637 ret = 0;
639 } else {
640 DEBUG(0,("Unable to cancel job %d - %s\n", pjob->sysjob,
641 ippErrorString(cupsLastError())));
644 out:
645 if (response)
646 ippDelete(response);
648 if (language)
649 cupsLangFree(language);
651 if (http)
652 httpClose(http);
654 TALLOC_FREE(frame);
655 return ret;
660 * 'cups_job_pause()' - Pause a job.
663 static int cups_job_pause(int snum, struct printjob *pjob)
665 TALLOC_CTX *frame = talloc_stackframe();
666 int ret = 1; /* Return value */
667 http_t *http = NULL; /* HTTP connection to server */
668 ipp_t *request = NULL, /* IPP Request */
669 *response = NULL; /* IPP Response */
670 cups_lang_t *language = NULL; /* Default language */
671 char *user = NULL;
672 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
675 DEBUG(5,("cups_job_pause(%d, %p (%d))\n", snum, pjob, pjob->sysjob));
678 * Make sure we don't ask for passwords...
681 cupsSetPasswordCB(cups_passwd_cb);
684 * Try to connect to the server...
687 if ((http = cups_connect(frame)) == NULL) {
688 goto out;
692 * Build an IPP_HOLD_JOB request, which requires the following
693 * attributes:
695 * attributes-charset
696 * attributes-natural-language
697 * job-uri
698 * requesting-user-name
701 request = ippNew();
703 request->request.op.operation_id = IPP_HOLD_JOB;
704 request->request.op.request_id = 1;
706 language = cupsLangDefault();
708 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
709 "attributes-charset", NULL, "utf-8");
711 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
712 "attributes-natural-language", NULL, language->language);
714 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/jobs/%d", pjob->sysjob);
716 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri);
718 if (push_utf8_talloc(frame, &user, pjob->user) == (size_t)-1) {
719 goto out;
721 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
722 NULL, user);
725 * Do the request and get back a response...
728 if ((response = cupsDoRequest(http, request, "/jobs")) != NULL) {
729 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
730 DEBUG(0,("Unable to hold job %d - %s\n", pjob->sysjob,
731 ippErrorString(cupsLastError())));
732 } else {
733 ret = 0;
735 } else {
736 DEBUG(0,("Unable to hold job %d - %s\n", pjob->sysjob,
737 ippErrorString(cupsLastError())));
740 out:
741 if (response)
742 ippDelete(response);
744 if (language)
745 cupsLangFree(language);
747 if (http)
748 httpClose(http);
750 TALLOC_FREE(frame);
751 return ret;
756 * 'cups_job_resume()' - Resume a paused job.
759 static int cups_job_resume(int snum, struct printjob *pjob)
761 TALLOC_CTX *frame = talloc_stackframe();
762 int ret = 1; /* Return value */
763 http_t *http = NULL; /* HTTP connection to server */
764 ipp_t *request = NULL, /* IPP Request */
765 *response = NULL; /* IPP Response */
766 cups_lang_t *language = NULL; /* Default language */
767 char *user = NULL;
768 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
771 DEBUG(5,("cups_job_resume(%d, %p (%d))\n", snum, pjob, pjob->sysjob));
774 * Make sure we don't ask for passwords...
777 cupsSetPasswordCB(cups_passwd_cb);
780 * Try to connect to the server...
783 if ((http = cups_connect(frame)) == NULL) {
784 goto out;
788 * Build an IPP_RELEASE_JOB request, which requires the following
789 * attributes:
791 * attributes-charset
792 * attributes-natural-language
793 * job-uri
794 * requesting-user-name
797 request = ippNew();
799 request->request.op.operation_id = IPP_RELEASE_JOB;
800 request->request.op.request_id = 1;
802 language = cupsLangDefault();
804 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
805 "attributes-charset", NULL, "utf-8");
807 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
808 "attributes-natural-language", NULL, language->language);
810 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/jobs/%d", pjob->sysjob);
812 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri);
814 if (push_utf8_talloc(frame, &user, pjob->user) == (size_t)-1) {
815 goto out;
817 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
818 NULL, user);
821 * Do the request and get back a response...
824 if ((response = cupsDoRequest(http, request, "/jobs")) != NULL) {
825 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
826 DEBUG(0,("Unable to release job %d - %s\n", pjob->sysjob,
827 ippErrorString(cupsLastError())));
828 } else {
829 ret = 0;
831 } else {
832 DEBUG(0,("Unable to release job %d - %s\n", pjob->sysjob,
833 ippErrorString(cupsLastError())));
836 out:
837 if (response)
838 ippDelete(response);
840 if (language)
841 cupsLangFree(language);
843 if (http)
844 httpClose(http);
846 TALLOC_FREE(frame);
847 return ret;
852 * 'cups_job_submit()' - Submit a job for printing.
855 static int cups_job_submit(int snum, struct printjob *pjob)
857 TALLOC_CTX *frame = talloc_stackframe();
858 int ret = 1; /* Return value */
859 http_t *http = NULL; /* HTTP connection to server */
860 ipp_t *request = NULL, /* IPP Request */
861 *response = NULL; /* IPP Response */
862 cups_lang_t *language = NULL; /* Default language */
863 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
864 const char *clientname = NULL; /* hostname of client for job-originating-host attribute */
865 char *new_jobname = NULL;
866 int num_options = 0;
867 cups_option_t *options = NULL;
868 char *printername = NULL;
869 char *user = NULL;
870 char *jobname = NULL;
871 char *cupsoptions = NULL;
872 char *filename = NULL;
873 char addr[INET6_ADDRSTRLEN];
875 DEBUG(5,("cups_job_submit(%d, %p (%d))\n", snum, pjob, pjob->sysjob));
878 * Make sure we don't ask for passwords...
881 cupsSetPasswordCB(cups_passwd_cb);
884 * Try to connect to the server...
887 if ((http = cups_connect(frame)) == NULL) {
888 goto out;
892 * Build an IPP_PRINT_JOB request, which requires the following
893 * attributes:
895 * attributes-charset
896 * attributes-natural-language
897 * printer-uri
898 * requesting-user-name
899 * [document-data]
902 request = ippNew();
904 request->request.op.operation_id = IPP_PRINT_JOB;
905 request->request.op.request_id = 1;
907 language = cupsLangDefault();
909 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
910 "attributes-charset", NULL, "utf-8");
912 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
913 "attributes-natural-language", NULL, language->language);
915 if (push_utf8_talloc(frame, &printername, PRINTERNAME(snum)) == (size_t)-1) {
916 goto out;
918 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s",
919 printername);
921 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
922 "printer-uri", NULL, uri);
924 if (push_utf8_talloc(frame, &user, pjob->user) == (size_t)-1) {
925 goto out;
927 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
928 NULL, user);
930 clientname = client_name(get_client_fd());
931 if (strcmp(clientname, "UNKNOWN") == 0) {
932 clientname = client_addr(get_client_fd(),addr,sizeof(addr));
935 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
936 "job-originating-host-name", NULL,
937 clientname);
939 if (push_utf8_talloc(frame, &jobname, pjob->jobname) == (size_t)-1) {
940 goto out;
942 new_jobname = talloc_asprintf(frame,
943 "%s%.8u %s", PRINT_SPOOL_PREFIX,
944 (unsigned int)pjob->smbjob,
945 jobname);
946 if (new_jobname == NULL) {
947 goto out;
950 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name", NULL,
951 new_jobname);
954 * add any options defined in smb.conf
957 if (push_utf8_talloc(frame, &cupsoptions, lp_cups_options(snum)) == (size_t)-1) {
958 goto out;
960 num_options = 0;
961 options = NULL;
962 num_options = cupsParseOptions(cupsoptions, num_options, &options);
964 if ( num_options )
965 cupsEncodeOptions(request, num_options, options);
968 * Do the request and get back a response...
971 slprintf(uri, sizeof(uri) - 1, "/printers/%s", printername);
973 if (push_utf8_talloc(frame, &filename, pjob->filename) == (size_t)-1) {
974 goto out;
976 if ((response = cupsDoFileRequest(http, request, uri, pjob->filename)) != NULL) {
977 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
978 DEBUG(0,("Unable to print file to %s - %s\n", PRINTERNAME(snum),
979 ippErrorString(cupsLastError())));
980 } else {
981 ret = 0;
983 } else {
984 DEBUG(0,("Unable to print file to `%s' - %s\n", PRINTERNAME(snum),
985 ippErrorString(cupsLastError())));
988 if ( ret == 0 )
989 unlink(pjob->filename);
990 /* else print_job_end will do it for us */
992 out:
993 if (response)
994 ippDelete(response);
996 if (language)
997 cupsLangFree(language);
999 if (http)
1000 httpClose(http);
1002 TALLOC_FREE(frame);
1004 return ret;
1008 * 'cups_queue_get()' - Get all the jobs in the print queue.
1011 static int cups_queue_get(const char *sharename,
1012 enum printing_types printing_type,
1013 char *lpq_command,
1014 print_queue_struct **q,
1015 print_status_struct *status)
1017 TALLOC_CTX *frame = talloc_stackframe();
1018 char *printername = NULL;
1019 http_t *http = NULL; /* HTTP connection to server */
1020 ipp_t *request = NULL, /* IPP Request */
1021 *response = NULL; /* IPP Response */
1022 ipp_attribute_t *attr = NULL; /* Current attribute */
1023 cups_lang_t *language = NULL; /* Default language */
1024 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
1025 int qcount = 0, /* Number of active queue entries */
1026 qalloc = 0; /* Number of queue entries allocated */
1027 print_queue_struct *queue = NULL, /* Queue entries */
1028 *temp; /* Temporary pointer for queue */
1029 char *user_name = NULL, /* job-originating-user-name attribute */
1030 *job_name = NULL; /* job-name attribute */
1031 int job_id; /* job-id attribute */
1032 int job_k_octets; /* job-k-octets attribute */
1033 time_t job_time; /* time-at-creation attribute */
1034 ipp_jstate_t job_status; /* job-status attribute */
1035 int job_priority; /* job-priority attribute */
1036 static const char *jattrs[] = /* Requested job attributes */
1038 "job-id",
1039 "job-k-octets",
1040 "job-name",
1041 "job-originating-user-name",
1042 "job-priority",
1043 "job-state",
1044 "time-at-creation",
1046 static const char *pattrs[] = /* Requested printer attributes */
1048 "printer-state",
1049 "printer-state-message"
1052 *q = NULL;
1054 /* HACK ALERT!!! The problem with support the 'printer name'
1055 option is that we key the tdb off the sharename. So we will
1056 overload the lpq_command string to pass in the printername
1057 (which is basically what we do for non-cups printers ... using
1058 the lpq_command to get the queue listing). */
1060 if (push_utf8_talloc(frame, &printername, lpq_command) == (size_t)-1) {
1061 goto out;
1063 DEBUG(5,("cups_queue_get(%s, %p, %p)\n", lpq_command, q, status));
1066 * Make sure we don't ask for passwords...
1069 cupsSetPasswordCB(cups_passwd_cb);
1072 * Try to connect to the server...
1075 if ((http = cups_connect(frame)) == NULL) {
1076 goto out;
1080 * Generate the printer URI...
1083 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s", printername);
1086 * Build an IPP_GET_JOBS request, which requires the following
1087 * attributes:
1089 * attributes-charset
1090 * attributes-natural-language
1091 * requested-attributes
1092 * printer-uri
1095 request = ippNew();
1097 request->request.op.operation_id = IPP_GET_JOBS;
1098 request->request.op.request_id = 1;
1100 language = cupsLangDefault();
1102 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1103 "attributes-charset", NULL, "utf-8");
1105 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1106 "attributes-natural-language", NULL, language->language);
1108 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1109 "requested-attributes",
1110 (sizeof(jattrs) / sizeof(jattrs[0])),
1111 NULL, jattrs);
1113 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1114 "printer-uri", NULL, uri);
1117 * Do the request and get back a response...
1120 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
1121 DEBUG(0,("Unable to get jobs for %s - %s\n", uri,
1122 ippErrorString(cupsLastError())));
1123 goto out;
1126 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1127 DEBUG(0,("Unable to get jobs for %s - %s\n", uri,
1128 ippErrorString(response->request.status.status_code)));
1129 goto out;
1133 * Process the jobs...
1136 qcount = 0;
1137 qalloc = 0;
1138 queue = NULL;
1140 for (attr = response->attrs; attr != NULL; attr = attr->next) {
1142 * Skip leading attributes until we hit a job...
1145 while (attr != NULL && attr->group_tag != IPP_TAG_JOB)
1146 attr = attr->next;
1148 if (attr == NULL)
1149 break;
1152 * Allocate memory as needed...
1154 if (qcount >= qalloc) {
1155 qalloc += 16;
1157 queue = SMB_REALLOC_ARRAY(queue, print_queue_struct, qalloc);
1159 if (queue == NULL) {
1160 DEBUG(0,("cups_queue_get: Not enough memory!"));
1161 qcount = 0;
1162 goto out;
1166 temp = queue + qcount;
1167 memset(temp, 0, sizeof(print_queue_struct));
1170 * Pull the needed attributes from this job...
1173 job_id = 0;
1174 job_priority = 50;
1175 job_status = IPP_JOB_PENDING;
1176 job_time = 0;
1177 job_k_octets = 0;
1178 user_name = NULL;
1179 job_name = NULL;
1181 while (attr != NULL && attr->group_tag == IPP_TAG_JOB) {
1182 if (attr->name == NULL) {
1183 attr = attr->next;
1184 break;
1187 if (strcmp(attr->name, "job-id") == 0 &&
1188 attr->value_tag == IPP_TAG_INTEGER)
1189 job_id = attr->values[0].integer;
1191 if (strcmp(attr->name, "job-k-octets") == 0 &&
1192 attr->value_tag == IPP_TAG_INTEGER)
1193 job_k_octets = attr->values[0].integer;
1195 if (strcmp(attr->name, "job-priority") == 0 &&
1196 attr->value_tag == IPP_TAG_INTEGER)
1197 job_priority = attr->values[0].integer;
1199 if (strcmp(attr->name, "job-state") == 0 &&
1200 attr->value_tag == IPP_TAG_ENUM)
1201 job_status = (ipp_jstate_t)(attr->values[0].integer);
1203 if (strcmp(attr->name, "time-at-creation") == 0 &&
1204 attr->value_tag == IPP_TAG_INTEGER)
1205 job_time = attr->values[0].integer;
1207 if (strcmp(attr->name, "job-name") == 0 &&
1208 attr->value_tag == IPP_TAG_NAME) {
1209 pull_utf8_talloc(frame,
1210 &job_name,
1211 attr->values[0].string.text);
1214 if (strcmp(attr->name, "job-originating-user-name") == 0 &&
1215 attr->value_tag == IPP_TAG_NAME) {
1216 pull_utf8_talloc(frame,
1217 &user_name,
1218 attr->values[0].string.text);
1221 attr = attr->next;
1225 * See if we have everything needed...
1228 if (user_name == NULL || job_name == NULL || job_id == 0) {
1229 if (attr == NULL)
1230 break;
1231 else
1232 continue;
1235 temp->job = job_id;
1236 temp->size = job_k_octets * 1024;
1237 temp->status = job_status == IPP_JOB_PENDING ? LPQ_QUEUED :
1238 job_status == IPP_JOB_STOPPED ? LPQ_PAUSED :
1239 job_status == IPP_JOB_HELD ? LPQ_PAUSED :
1240 LPQ_PRINTING;
1241 temp->priority = job_priority;
1242 temp->time = job_time;
1243 strlcpy(temp->fs_user, user_name, sizeof(temp->fs_user));
1244 strlcpy(temp->fs_file, job_name, sizeof(temp->fs_file));
1246 qcount ++;
1248 if (attr == NULL)
1249 break;
1252 ippDelete(response);
1253 response = NULL;
1256 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the
1257 * following attributes:
1259 * attributes-charset
1260 * attributes-natural-language
1261 * requested-attributes
1262 * printer-uri
1265 request = ippNew();
1267 request->request.op.operation_id = IPP_GET_PRINTER_ATTRIBUTES;
1268 request->request.op.request_id = 1;
1270 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1271 "attributes-charset", NULL, "utf-8");
1273 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1274 "attributes-natural-language", NULL, language->language);
1276 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1277 "requested-attributes",
1278 (sizeof(pattrs) / sizeof(pattrs[0])),
1279 NULL, pattrs);
1281 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1282 "printer-uri", NULL, uri);
1285 * Do the request and get back a response...
1288 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
1289 DEBUG(0,("Unable to get printer status for %s - %s\n", printername,
1290 ippErrorString(cupsLastError())));
1291 *q = queue;
1292 goto out;
1295 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1296 DEBUG(0,("Unable to get printer status for %s - %s\n", printername,
1297 ippErrorString(response->request.status.status_code)));
1298 *q = queue;
1299 goto out;
1303 * Get the current printer status and convert it to the SAMBA values.
1306 if ((attr = ippFindAttribute(response, "printer-state", IPP_TAG_ENUM)) != NULL) {
1307 if (attr->values[0].integer == IPP_PRINTER_STOPPED)
1308 status->status = LPSTAT_STOPPED;
1309 else
1310 status->status = LPSTAT_OK;
1313 if ((attr = ippFindAttribute(response, "printer-state-message",
1314 IPP_TAG_TEXT)) != NULL) {
1315 char *msg = NULL;
1316 pull_utf8_talloc(frame, &msg, attr->values[0].string.text);
1317 fstrcpy(status->message, msg);
1321 * Return the job queue...
1324 *q = queue;
1326 out:
1327 if (response)
1328 ippDelete(response);
1330 if (language)
1331 cupsLangFree(language);
1333 if (http)
1334 httpClose(http);
1336 TALLOC_FREE(frame);
1337 return qcount;
1342 * 'cups_queue_pause()' - Pause a print queue.
1345 static int cups_queue_pause(int snum)
1347 TALLOC_CTX *frame = talloc_stackframe();
1348 int ret = 1; /* Return value */
1349 http_t *http = NULL; /* HTTP connection to server */
1350 ipp_t *request = NULL, /* IPP Request */
1351 *response = NULL; /* IPP Response */
1352 cups_lang_t *language = NULL; /* Default language */
1353 char *printername = NULL;
1354 char *username = NULL;
1355 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
1358 DEBUG(5,("cups_queue_pause(%d)\n", snum));
1361 * Make sure we don't ask for passwords...
1364 cupsSetPasswordCB(cups_passwd_cb);
1367 * Try to connect to the server...
1370 if ((http = cups_connect(frame)) == NULL) {
1371 goto out;
1375 * Build an IPP_PAUSE_PRINTER request, which requires the following
1376 * attributes:
1378 * attributes-charset
1379 * attributes-natural-language
1380 * printer-uri
1381 * requesting-user-name
1384 request = ippNew();
1386 request->request.op.operation_id = IPP_PAUSE_PRINTER;
1387 request->request.op.request_id = 1;
1389 language = cupsLangDefault();
1391 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1392 "attributes-charset", NULL, "utf-8");
1394 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1395 "attributes-natural-language", NULL, language->language);
1397 if (push_utf8_talloc(frame, &printername, PRINTERNAME(snum)) == (size_t)-1) {
1398 goto out;
1400 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s",
1401 printername);
1403 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
1405 if (push_utf8_talloc(frame, &username, current_user_info.unix_name) == (size_t)-1) {
1406 goto out;
1408 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
1409 NULL, username);
1412 * Do the request and get back a response...
1415 if ((response = cupsDoRequest(http, request, "/admin/")) != NULL) {
1416 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1417 DEBUG(0,("Unable to pause printer %s - %s\n", PRINTERNAME(snum),
1418 ippErrorString(cupsLastError())));
1419 } else {
1420 ret = 0;
1422 } else {
1423 DEBUG(0,("Unable to pause printer %s - %s\n", PRINTERNAME(snum),
1424 ippErrorString(cupsLastError())));
1427 out:
1428 if (response)
1429 ippDelete(response);
1431 if (language)
1432 cupsLangFree(language);
1434 if (http)
1435 httpClose(http);
1437 TALLOC_FREE(frame);
1438 return ret;
1443 * 'cups_queue_resume()' - Restart a print queue.
1446 static int cups_queue_resume(int snum)
1448 TALLOC_CTX *frame = talloc_stackframe();
1449 int ret = 1; /* Return value */
1450 http_t *http = NULL; /* HTTP connection to server */
1451 ipp_t *request = NULL, /* IPP Request */
1452 *response = NULL; /* IPP Response */
1453 cups_lang_t *language = NULL; /* Default language */
1454 char *printername = NULL;
1455 char *username = NULL;
1456 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
1459 DEBUG(5,("cups_queue_resume(%d)\n", snum));
1462 * Make sure we don't ask for passwords...
1465 cupsSetPasswordCB(cups_passwd_cb);
1468 * Try to connect to the server...
1471 if ((http = cups_connect(frame)) == NULL) {
1472 goto out;
1476 * Build an IPP_RESUME_PRINTER request, which requires the following
1477 * attributes:
1479 * attributes-charset
1480 * attributes-natural-language
1481 * printer-uri
1482 * requesting-user-name
1485 request = ippNew();
1487 request->request.op.operation_id = IPP_RESUME_PRINTER;
1488 request->request.op.request_id = 1;
1490 language = cupsLangDefault();
1492 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1493 "attributes-charset", NULL, "utf-8");
1495 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1496 "attributes-natural-language", NULL, language->language);
1498 if (push_utf8_talloc(frame, &printername, PRINTERNAME(snum)) == (size_t)-1) {
1499 goto out;
1501 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s",
1502 printername);
1504 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
1506 if (push_utf8_talloc(frame, &username, current_user_info.unix_name) == (size_t)-1) {
1507 goto out;
1509 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
1510 NULL, username);
1513 * Do the request and get back a response...
1516 if ((response = cupsDoRequest(http, request, "/admin/")) != NULL) {
1517 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1518 DEBUG(0,("Unable to resume printer %s - %s\n", PRINTERNAME(snum),
1519 ippErrorString(cupsLastError())));
1520 } else {
1521 ret = 0;
1523 } else {
1524 DEBUG(0,("Unable to resume printer %s - %s\n", PRINTERNAME(snum),
1525 ippErrorString(cupsLastError())));
1528 out:
1529 if (response)
1530 ippDelete(response);
1532 if (language)
1533 cupsLangFree(language);
1535 if (http)
1536 httpClose(http);
1538 TALLOC_FREE(frame);
1539 return ret;
1542 /*******************************************************************
1543 * CUPS printing interface definitions...
1544 ******************************************************************/
1546 struct printif cups_printif =
1548 PRINT_CUPS,
1549 cups_queue_get,
1550 cups_queue_pause,
1551 cups_queue_resume,
1552 cups_job_delete,
1553 cups_job_pause,
1554 cups_job_resume,
1555 cups_job_submit,
1558 bool cups_pull_comment_location(NT_PRINTER_INFO_LEVEL_2 *printer)
1560 TALLOC_CTX *frame = talloc_stackframe();
1561 http_t *http = NULL; /* HTTP connection to server */
1562 ipp_t *request = NULL, /* IPP Request */
1563 *response = NULL; /* IPP Response */
1564 ipp_attribute_t *attr; /* Current attribute */
1565 cups_lang_t *language = NULL; /* Default language */
1566 char uri[HTTP_MAX_URI];
1567 char *server = NULL;
1568 char *sharename = NULL;
1569 char *name = NULL;
1570 static const char *requested[] =/* Requested attributes */
1572 "printer-name",
1573 "printer-info",
1574 "printer-location"
1576 bool ret = False;
1578 DEBUG(5, ("pulling %s location\n", printer->sharename));
1581 * Make sure we don't ask for passwords...
1584 cupsSetPasswordCB(cups_passwd_cb);
1587 * Try to connect to the server...
1590 if ((http = cups_connect(frame)) == NULL) {
1591 goto out;
1594 request = ippNew();
1596 request->request.op.operation_id = IPP_GET_PRINTER_ATTRIBUTES;
1597 request->request.op.request_id = 1;
1599 language = cupsLangDefault();
1601 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1602 "attributes-charset", NULL, "utf-8");
1604 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1605 "attributes-natural-language", NULL, language->language);
1607 if (lp_cups_server() != NULL && strlen(lp_cups_server()) > 0) {
1608 if (push_utf8_talloc(frame, &server, lp_cups_server()) == (size_t)-1) {
1609 goto out;
1611 } else {
1612 server = talloc_strdup(frame,cupsServer());
1614 if (server) {
1615 goto out;
1617 if (push_utf8_talloc(frame, &sharename, printer->sharename) == (size_t)-1) {
1618 goto out;
1620 slprintf(uri, sizeof(uri) - 1, "ipp://%s/printers/%s",
1621 server, sharename);
1623 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1624 "printer-uri", NULL, uri);
1626 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1627 "requested-attributes",
1628 (sizeof(requested) / sizeof(requested[0])),
1629 NULL, requested);
1632 * Do the request and get back a response...
1635 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
1636 DEBUG(0,("Unable to get printer attributes - %s\n",
1637 ippErrorString(cupsLastError())));
1638 goto out;
1641 for (attr = response->attrs; attr != NULL;) {
1643 * Skip leading attributes until we hit a printer...
1646 while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)
1647 attr = attr->next;
1649 if (attr == NULL)
1650 break;
1653 * Pull the needed attributes from this printer...
1656 while ( attr && (attr->group_tag == IPP_TAG_PRINTER) ) {
1657 if (strcmp(attr->name, "printer-name") == 0 &&
1658 attr->value_tag == IPP_TAG_NAME) {
1659 pull_utf8_talloc(frame,
1660 &name,
1661 attr->values[0].string.text);
1664 /* Grab the comment if we don't have one */
1665 if ( (strcmp(attr->name, "printer-info") == 0)
1666 && (attr->value_tag == IPP_TAG_TEXT)
1667 && !strlen(printer->comment) )
1669 char *comment = NULL;
1670 pull_utf8_talloc(frame,
1671 &comment,
1672 attr->values[0].string.text);
1673 DEBUG(5,("cups_pull_comment_location: Using cups comment: %s\n",
1674 comment));
1675 strlcpy(printer->comment,
1676 comment,
1677 sizeof(printer->comment));
1680 /* Grab the location if we don't have one */
1681 if ( (strcmp(attr->name, "printer-location") == 0)
1682 && (attr->value_tag == IPP_TAG_TEXT)
1683 && !strlen(printer->location) )
1685 char *location = NULL;
1686 pull_utf8_talloc(frame,
1687 &location,
1688 attr->values[0].string.text);
1689 DEBUG(5,("cups_pull_comment_location: Using cups location: %s\n",
1690 location));
1691 strlcpy(printer->location,
1692 location,
1693 sizeof(printer->location));
1696 attr = attr->next;
1700 * We have everything needed...
1703 if (name != NULL)
1704 break;
1707 ret = True;
1709 out:
1710 if (response)
1711 ippDelete(response);
1713 if (language)
1714 cupsLangFree(language);
1716 if (http)
1717 httpClose(http);
1719 TALLOC_FREE(frame);
1720 return ret;
1723 #else
1724 /* this keeps fussy compilers happy */
1725 void print_cups_dummy(void);
1726 void print_cups_dummy(void) {}
1727 #endif /* HAVE_CUPS */