s3-printing: reload shares after pcap cache fill
[Samba.git] / source3 / printing / print_cups.c
blob82b431427ff7ba681868eea0045a8442b54cecb0
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 static SIG_ATOMIC_T gotalarm;
34 /***************************************************************
35 Signal function to tell us we timed out.
36 ****************************************************************/
38 static void gotalarm_sig(void)
40 gotalarm = 1;
43 extern userdom_struct current_user_info;
46 * 'cups_passwd_cb()' - The CUPS password callback...
49 static const char * /* O - Password or NULL */
50 cups_passwd_cb(const char *prompt) /* I - Prompt */
53 * Always return NULL to indicate that no password is available...
56 return (NULL);
59 static http_t *cups_connect(TALLOC_CTX *frame)
61 http_t *http = NULL;
62 char *server = NULL, *p = NULL;
63 int port;
64 int timeout = lp_cups_connection_timeout();
65 size_t size;
67 if (lp_cups_server() != NULL && strlen(lp_cups_server()) > 0) {
68 if (!push_utf8_talloc(frame, &server, lp_cups_server(), &size)) {
69 return NULL;
71 } else {
72 server = talloc_strdup(frame,cupsServer());
74 if (!server) {
75 return NULL;
78 p = strchr(server, ':');
79 if (p) {
80 port = atoi(p+1);
81 *p = '\0';
82 } else {
83 port = ippPort();
86 DEBUG(10, ("connecting to cups server %s:%d\n",
87 server, port));
89 gotalarm = 0;
91 if (timeout) {
92 CatchSignal(SIGALRM, SIGNAL_CAST gotalarm_sig);
93 alarm(timeout);
96 #ifdef HAVE_HTTPCONNECTENCRYPT
97 http = httpConnectEncrypt(server, port, lp_cups_encrypt());
98 #else
99 http = httpConnect(server, port);
100 #endif
103 CatchSignal(SIGALRM, SIGNAL_CAST SIG_IGN);
104 alarm(0);
106 if (http == NULL) {
107 DEBUG(0,("Unable to connect to CUPS server %s:%d - %s\n",
108 server, port, strerror(errno)));
111 return http;
114 static void send_pcap_info(const char *name, const char *info, void *pd)
116 int fd = *(int *)pd;
117 size_t namelen = name ? strlen(name)+1 : 0;
118 size_t infolen = info ? strlen(info)+1 : 0;
120 DEBUG(11,("send_pcap_info: writing namelen %u\n", (unsigned int)namelen));
121 if (sys_write(fd, &namelen, sizeof(namelen)) != sizeof(namelen)) {
122 DEBUG(10,("send_pcap_info: namelen write failed %s\n",
123 strerror(errno)));
124 return;
126 DEBUG(11,("send_pcap_info: writing infolen %u\n", (unsigned int)infolen));
127 if (sys_write(fd, &infolen, sizeof(infolen)) != sizeof(infolen)) {
128 DEBUG(10,("send_pcap_info: infolen write failed %s\n",
129 strerror(errno)));
130 return;
132 if (namelen) {
133 DEBUG(11,("send_pcap_info: writing name %s\n", name));
134 if (sys_write(fd, name, namelen) != namelen) {
135 DEBUG(10,("send_pcap_info: name write failed %s\n",
136 strerror(errno)));
137 return;
140 if (infolen) {
141 DEBUG(11,("send_pcap_info: writing info %s\n", info));
142 if (sys_write(fd, info, infolen) != infolen) {
143 DEBUG(10,("send_pcap_info: info write failed %s\n",
144 strerror(errno)));
145 return;
150 static bool cups_cache_reload_async(int fd)
152 TALLOC_CTX *frame = talloc_stackframe();
153 struct pcap_cache *tmp_pcap_cache = NULL;
154 http_t *http = NULL; /* HTTP connection to server */
155 ipp_t *request = NULL, /* IPP Request */
156 *response = NULL; /* IPP Response */
157 ipp_attribute_t *attr; /* Current attribute */
158 cups_lang_t *language = NULL; /* Default language */
159 char *name, /* printer-name attribute */
160 *info; /* printer-info attribute */
161 static const char *requested[] =/* Requested attributes */
163 "printer-name",
164 "printer-info"
166 bool ret = False;
167 size_t size;
169 DEBUG(5, ("reloading cups printcap cache\n"));
172 * Make sure we don't ask for passwords...
175 cupsSetPasswordCB(cups_passwd_cb);
178 * Try to connect to the server...
181 if ((http = cups_connect(frame)) == NULL) {
182 goto out;
186 * Build a CUPS_GET_PRINTERS request, which requires the following
187 * attributes:
189 * attributes-charset
190 * attributes-natural-language
191 * requested-attributes
194 request = ippNew();
196 request->request.op.operation_id = CUPS_GET_PRINTERS;
197 request->request.op.request_id = 1;
199 language = cupsLangDefault();
201 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
202 "attributes-charset", NULL, "utf-8");
204 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
205 "attributes-natural-language", NULL, language->language);
207 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
208 "requested-attributes",
209 (sizeof(requested) / sizeof(requested[0])),
210 NULL, requested);
213 * Do the request and get back a response...
216 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
217 DEBUG(0,("Unable to get printer list - %s\n",
218 ippErrorString(cupsLastError())));
219 goto out;
222 for (attr = response->attrs; attr != NULL;) {
224 * Skip leading attributes until we hit a printer...
227 while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)
228 attr = attr->next;
230 if (attr == NULL)
231 break;
234 * Pull the needed attributes from this printer...
237 name = NULL;
238 info = NULL;
240 while (attr != NULL && attr->group_tag == IPP_TAG_PRINTER) {
241 if (strcmp(attr->name, "printer-name") == 0 &&
242 attr->value_tag == IPP_TAG_NAME) {
243 if (!pull_utf8_talloc(frame,
244 &name,
245 attr->values[0].string.text,
246 &size)) {
247 goto out;
251 if (strcmp(attr->name, "printer-info") == 0 &&
252 attr->value_tag == IPP_TAG_TEXT) {
253 if (!pull_utf8_talloc(frame,
254 &info,
255 attr->values[0].string.text,
256 &size)) {
257 goto out;
261 attr = attr->next;
265 * See if we have everything needed...
268 if (name == NULL)
269 break;
271 if (!pcap_cache_add_specific(&tmp_pcap_cache, name, info)) {
272 goto out;
276 ippDelete(response);
277 response = NULL;
280 * Build a CUPS_GET_CLASSES request, which requires the following
281 * attributes:
283 * attributes-charset
284 * attributes-natural-language
285 * requested-attributes
288 request = ippNew();
290 request->request.op.operation_id = CUPS_GET_CLASSES;
291 request->request.op.request_id = 1;
293 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
294 "attributes-charset", NULL, "utf-8");
296 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
297 "attributes-natural-language", NULL, language->language);
299 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
300 "requested-attributes",
301 (sizeof(requested) / sizeof(requested[0])),
302 NULL, requested);
305 * Do the request and get back a response...
308 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
309 DEBUG(0,("Unable to get printer list - %s\n",
310 ippErrorString(cupsLastError())));
311 goto out;
314 for (attr = response->attrs; attr != NULL;) {
316 * Skip leading attributes until we hit a printer...
319 while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)
320 attr = attr->next;
322 if (attr == NULL)
323 break;
326 * Pull the needed attributes from this printer...
329 name = NULL;
330 info = NULL;
332 while (attr != NULL && attr->group_tag == IPP_TAG_PRINTER) {
333 if (strcmp(attr->name, "printer-name") == 0 &&
334 attr->value_tag == IPP_TAG_NAME) {
335 if (!pull_utf8_talloc(frame,
336 &name,
337 attr->values[0].string.text,
338 &size)) {
339 goto out;
343 if (strcmp(attr->name, "printer-info") == 0 &&
344 attr->value_tag == IPP_TAG_TEXT) {
345 if (!pull_utf8_talloc(frame,
346 &info,
347 attr->values[0].string.text,
348 &size)) {
349 goto out;
353 attr = attr->next;
357 * See if we have everything needed...
360 if (name == NULL)
361 break;
363 if (!pcap_cache_add_specific(&tmp_pcap_cache, name, info)) {
364 goto out;
368 ret = True;
370 out:
371 if (response)
372 ippDelete(response);
374 if (language)
375 cupsLangFree(language);
377 if (http)
378 httpClose(http);
380 /* Send all the entries up the pipe. */
381 if (tmp_pcap_cache) {
382 pcap_printer_fn_specific(tmp_pcap_cache,
383 send_pcap_info,
384 (void *)&fd);
386 pcap_cache_destroy_specific(&tmp_pcap_cache);
388 TALLOC_FREE(frame);
389 return ret;
392 static struct pcap_cache *local_pcap_copy;
393 struct fd_event *cache_fd_event;
395 static bool cups_pcap_load_async(int *pfd)
397 int fds[2];
398 pid_t pid;
400 *pfd = -1;
402 if (cache_fd_event) {
403 DEBUG(3,("cups_pcap_load_async: already waiting for "
404 "a refresh event\n" ));
405 return false;
408 DEBUG(5,("cups_pcap_load_async: asynchronously loading cups printers\n"));
410 if (pipe(fds) == -1) {
411 return false;
414 pid = sys_fork();
415 if (pid == (pid_t)-1) {
416 DEBUG(10,("cups_pcap_load_async: fork failed %s\n",
417 strerror(errno) ));
418 close(fds[0]);
419 close(fds[1]);
420 return false;
423 if (pid) {
424 DEBUG(10,("cups_pcap_load_async: child pid = %u\n",
425 (unsigned int)pid ));
426 /* Parent. */
427 close(fds[1]);
428 *pfd = fds[0];
429 return true;
432 /* Child. */
434 close_all_print_db();
436 if (!NT_STATUS_IS_OK(reinit_after_fork(smbd_messaging_context(),
437 smbd_event_context(), true))) {
438 DEBUG(0,("cups_pcap_load_async: reinit_after_fork() failed\n"));
439 smb_panic("cups_pcap_load_async: reinit_after_fork() failed");
442 close(fds[0]);
443 cups_cache_reload_async(fds[1]);
444 close(fds[1]);
445 _exit(0);
448 struct cups_async_cb_args {
449 int pipe_fd;
450 void (*post_cache_fill_fn)(void);
453 static void cups_async_callback(struct event_context *event_ctx,
454 struct fd_event *event,
455 uint16 flags,
456 void *p)
458 TALLOC_CTX *frame = talloc_stackframe();
459 struct cups_async_cb_args *cb_args = (struct cups_async_cb_args *)p;
460 int fd = cb_args->pipe_fd;
461 struct pcap_cache *tmp_pcap_cache = NULL;
463 DEBUG(5,("cups_async_callback: callback received for printer data. "
464 "fd = %d\n", fd));
466 while (1) {
467 char *name = NULL, *info = NULL;
468 size_t namelen = 0, infolen = 0;
469 ssize_t ret = -1;
471 ret = sys_read(fd, &namelen, sizeof(namelen));
472 if (ret == 0) {
473 /* EOF */
474 break;
476 if (ret != sizeof(namelen)) {
477 DEBUG(10,("cups_async_callback: namelen read failed %d %s\n",
478 errno, strerror(errno)));
479 break;
482 DEBUG(11,("cups_async_callback: read namelen %u\n",
483 (unsigned int)namelen));
485 ret = sys_read(fd, &infolen, sizeof(infolen));
486 if (ret == 0) {
487 /* EOF */
488 break;
490 if (ret != sizeof(infolen)) {
491 DEBUG(10,("cups_async_callback: infolen read failed %s\n",
492 strerror(errno)));
493 break;
496 DEBUG(11,("cups_async_callback: read infolen %u\n",
497 (unsigned int)infolen));
499 if (namelen) {
500 name = TALLOC_ARRAY(frame, char, namelen);
501 if (!name) {
502 break;
504 ret = sys_read(fd, name, namelen);
505 if (ret == 0) {
506 /* EOF */
507 break;
509 if (ret != namelen) {
510 DEBUG(10,("cups_async_callback: name read failed %s\n",
511 strerror(errno)));
512 break;
514 DEBUG(11,("cups_async_callback: read name %s\n",
515 name));
516 } else {
517 name = NULL;
519 if (infolen) {
520 info = TALLOC_ARRAY(frame, char, infolen);
521 if (!info) {
522 break;
524 ret = sys_read(fd, info, infolen);
525 if (ret == 0) {
526 /* EOF */
527 break;
529 if (ret != infolen) {
530 DEBUG(10,("cups_async_callback: info read failed %s\n",
531 strerror(errno)));
532 break;
534 DEBUG(11,("cups_async_callback: read info %s\n",
535 info));
536 } else {
537 info = NULL;
540 /* Add to our local pcap cache. */
541 pcap_cache_add_specific(&tmp_pcap_cache, name, info);
542 TALLOC_FREE(name);
543 TALLOC_FREE(info);
546 TALLOC_FREE(frame);
547 if (tmp_pcap_cache) {
548 /* We got a namelist, replace our local cache. */
549 pcap_cache_destroy_specific(&local_pcap_copy);
550 local_pcap_copy = tmp_pcap_cache;
552 /* And the systemwide pcap cache. */
553 pcap_cache_replace(local_pcap_copy);
555 /* Caller may have requested post cache fill callback */
556 if (cb_args->post_cache_fill_fn) {
557 cb_args->post_cache_fill_fn();
559 } else {
560 DEBUG(2,("cups_async_callback: failed to read a new "
561 "printer list\n"));
563 close(fd);
564 TALLOC_FREE(cb_args);
565 TALLOC_FREE(cache_fd_event);
568 bool cups_cache_reload(void (*post_cache_fill_fn)(void))
570 struct cups_async_cb_args *cb_args;
571 int *p_pipe_fd;
573 cb_args = TALLOC_P(NULL, struct cups_async_cb_args);
574 if (!cb_args) {
575 return false;
577 cb_args->post_cache_fill_fn = post_cache_fill_fn;
578 p_pipe_fd = &cb_args->pipe_fd;
579 *p_pipe_fd = -1;
581 /* Set up an async refresh. */
582 if (!cups_pcap_load_async(p_pipe_fd)) {
583 talloc_free(cb_args);
584 return false;
586 if (!local_pcap_copy) {
587 /* We have no local cache, wait directly for
588 * async refresh to complete.
590 DEBUG(10,("cups_cache_reload: sync read on fd %d\n",
591 *p_pipe_fd ));
593 cups_async_callback(smbd_event_context(),
594 NULL,
595 EVENT_FD_READ,
596 (void *)cb_args);
597 if (!local_pcap_copy) {
598 return false;
600 } else {
601 /* Replace the system cache with our
602 * local copy. */
603 pcap_cache_replace(local_pcap_copy);
605 DEBUG(10,("cups_cache_reload: async read on fd %d\n",
606 *p_pipe_fd ));
608 /* Trigger an event when the pipe can be read. */
609 cache_fd_event = event_add_fd(smbd_event_context(),
610 NULL, *p_pipe_fd,
611 EVENT_FD_READ,
612 cups_async_callback,
613 (void *)cb_args);
614 if (!cache_fd_event) {
615 close(*p_pipe_fd);
616 talloc_free(cb_args);
617 return false;
620 return true;
624 * 'cups_job_delete()' - Delete a job.
627 static int cups_job_delete(const char *sharename, const char *lprm_command, struct printjob *pjob)
629 TALLOC_CTX *frame = talloc_stackframe();
630 int ret = 1; /* Return value */
631 http_t *http = NULL; /* HTTP connection to server */
632 ipp_t *request = NULL, /* IPP Request */
633 *response = NULL; /* IPP Response */
634 cups_lang_t *language = NULL; /* Default language */
635 char *user = NULL;
636 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
637 size_t size;
639 DEBUG(5,("cups_job_delete(%s, %p (%d))\n", sharename, pjob, pjob->sysjob));
642 * Make sure we don't ask for passwords...
645 cupsSetPasswordCB(cups_passwd_cb);
648 * Try to connect to the server...
651 if ((http = cups_connect(frame)) == NULL) {
652 goto out;
656 * Build an IPP_CANCEL_JOB request, which requires the following
657 * attributes:
659 * attributes-charset
660 * attributes-natural-language
661 * job-uri
662 * requesting-user-name
665 request = ippNew();
667 request->request.op.operation_id = IPP_CANCEL_JOB;
668 request->request.op.request_id = 1;
670 language = cupsLangDefault();
672 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
673 "attributes-charset", NULL, "utf-8");
675 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
676 "attributes-natural-language", NULL, language->language);
678 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/jobs/%d", pjob->sysjob);
680 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri);
682 if (!push_utf8_talloc(frame, &user, pjob->user, &size)) {
683 goto out;
686 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
687 NULL, user);
690 * Do the request and get back a response...
693 if ((response = cupsDoRequest(http, request, "/jobs")) != NULL) {
694 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
695 DEBUG(0,("Unable to cancel job %d - %s\n", pjob->sysjob,
696 ippErrorString(cupsLastError())));
697 } else {
698 ret = 0;
700 } else {
701 DEBUG(0,("Unable to cancel job %d - %s\n", pjob->sysjob,
702 ippErrorString(cupsLastError())));
705 out:
706 if (response)
707 ippDelete(response);
709 if (language)
710 cupsLangFree(language);
712 if (http)
713 httpClose(http);
715 TALLOC_FREE(frame);
716 return ret;
721 * 'cups_job_pause()' - Pause a job.
724 static int cups_job_pause(int snum, struct printjob *pjob)
726 TALLOC_CTX *frame = talloc_stackframe();
727 int ret = 1; /* Return value */
728 http_t *http = NULL; /* HTTP connection to server */
729 ipp_t *request = NULL, /* IPP Request */
730 *response = NULL; /* IPP Response */
731 cups_lang_t *language = NULL; /* Default language */
732 char *user = NULL;
733 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
734 size_t size;
736 DEBUG(5,("cups_job_pause(%d, %p (%d))\n", snum, pjob, pjob->sysjob));
739 * Make sure we don't ask for passwords...
742 cupsSetPasswordCB(cups_passwd_cb);
745 * Try to connect to the server...
748 if ((http = cups_connect(frame)) == NULL) {
749 goto out;
753 * Build an IPP_HOLD_JOB request, which requires the following
754 * attributes:
756 * attributes-charset
757 * attributes-natural-language
758 * job-uri
759 * requesting-user-name
762 request = ippNew();
764 request->request.op.operation_id = IPP_HOLD_JOB;
765 request->request.op.request_id = 1;
767 language = cupsLangDefault();
769 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
770 "attributes-charset", NULL, "utf-8");
772 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
773 "attributes-natural-language", NULL, language->language);
775 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/jobs/%d", pjob->sysjob);
777 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri);
779 if (!push_utf8_talloc(frame, &user, pjob->user, &size)) {
780 goto out;
782 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
783 NULL, user);
786 * Do the request and get back a response...
789 if ((response = cupsDoRequest(http, request, "/jobs")) != NULL) {
790 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
791 DEBUG(0,("Unable to hold job %d - %s\n", pjob->sysjob,
792 ippErrorString(cupsLastError())));
793 } else {
794 ret = 0;
796 } else {
797 DEBUG(0,("Unable to hold job %d - %s\n", pjob->sysjob,
798 ippErrorString(cupsLastError())));
801 out:
802 if (response)
803 ippDelete(response);
805 if (language)
806 cupsLangFree(language);
808 if (http)
809 httpClose(http);
811 TALLOC_FREE(frame);
812 return ret;
817 * 'cups_job_resume()' - Resume a paused job.
820 static int cups_job_resume(int snum, struct printjob *pjob)
822 TALLOC_CTX *frame = talloc_stackframe();
823 int ret = 1; /* Return value */
824 http_t *http = NULL; /* HTTP connection to server */
825 ipp_t *request = NULL, /* IPP Request */
826 *response = NULL; /* IPP Response */
827 cups_lang_t *language = NULL; /* Default language */
828 char *user = NULL;
829 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
830 size_t size;
832 DEBUG(5,("cups_job_resume(%d, %p (%d))\n", snum, pjob, pjob->sysjob));
835 * Make sure we don't ask for passwords...
838 cupsSetPasswordCB(cups_passwd_cb);
841 * Try to connect to the server...
844 if ((http = cups_connect(frame)) == NULL) {
845 goto out;
849 * Build an IPP_RELEASE_JOB request, which requires the following
850 * attributes:
852 * attributes-charset
853 * attributes-natural-language
854 * job-uri
855 * requesting-user-name
858 request = ippNew();
860 request->request.op.operation_id = IPP_RELEASE_JOB;
861 request->request.op.request_id = 1;
863 language = cupsLangDefault();
865 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
866 "attributes-charset", NULL, "utf-8");
868 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
869 "attributes-natural-language", NULL, language->language);
871 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/jobs/%d", pjob->sysjob);
873 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri);
875 if (!push_utf8_talloc(frame, &user, pjob->user, &size)) {
876 goto out;
878 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
879 NULL, user);
882 * Do the request and get back a response...
885 if ((response = cupsDoRequest(http, request, "/jobs")) != NULL) {
886 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
887 DEBUG(0,("Unable to release job %d - %s\n", pjob->sysjob,
888 ippErrorString(cupsLastError())));
889 } else {
890 ret = 0;
892 } else {
893 DEBUG(0,("Unable to release job %d - %s\n", pjob->sysjob,
894 ippErrorString(cupsLastError())));
897 out:
898 if (response)
899 ippDelete(response);
901 if (language)
902 cupsLangFree(language);
904 if (http)
905 httpClose(http);
907 TALLOC_FREE(frame);
908 return ret;
913 * 'cups_job_submit()' - Submit a job for printing.
916 static int cups_job_submit(int snum, struct printjob *pjob)
918 TALLOC_CTX *frame = talloc_stackframe();
919 int ret = 1; /* Return value */
920 http_t *http = NULL; /* HTTP connection to server */
921 ipp_t *request = NULL, /* IPP Request */
922 *response = NULL; /* IPP Response */
923 ipp_attribute_t *attr_job_id = NULL; /* IPP Attribute "job-id" */
924 cups_lang_t *language = NULL; /* Default language */
925 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
926 const char *clientname = NULL; /* hostname of client for job-originating-host attribute */
927 char *new_jobname = NULL;
928 int num_options = 0;
929 cups_option_t *options = NULL;
930 char *printername = NULL;
931 char *user = NULL;
932 char *jobname = NULL;
933 char *cupsoptions = NULL;
934 char *filename = NULL;
935 size_t size;
936 uint32_t jobid = (uint32_t)-1;
937 char addr[INET6_ADDRSTRLEN];
939 DEBUG(5,("cups_job_submit(%d, %p)\n", snum, pjob));
942 * Make sure we don't ask for passwords...
945 cupsSetPasswordCB(cups_passwd_cb);
948 * Try to connect to the server...
951 if ((http = cups_connect(frame)) == NULL) {
952 goto out;
956 * Build an IPP_PRINT_JOB request, which requires the following
957 * attributes:
959 * attributes-charset
960 * attributes-natural-language
961 * printer-uri
962 * requesting-user-name
963 * [document-data]
966 request = ippNew();
968 request->request.op.operation_id = IPP_PRINT_JOB;
969 request->request.op.request_id = 1;
971 language = cupsLangDefault();
973 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
974 "attributes-charset", NULL, "utf-8");
976 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
977 "attributes-natural-language", NULL, language->language);
979 if (!push_utf8_talloc(frame, &printername, PRINTERNAME(snum), &size)) {
980 goto out;
982 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s",
983 printername);
985 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
986 "printer-uri", NULL, uri);
988 if (!push_utf8_talloc(frame, &user, pjob->user, &size)) {
989 goto out;
991 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
992 NULL, user);
994 clientname = client_name(get_client_fd());
995 if (strcmp(clientname, "UNKNOWN") == 0) {
996 clientname = client_addr(get_client_fd(),addr,sizeof(addr));
999 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1000 "job-originating-host-name", NULL,
1001 clientname);
1003 /* Get the jobid from the filename. */
1004 jobid = print_parse_jobid(pjob->filename);
1005 if (jobid == (uint32_t)-1) {
1006 DEBUG(0,("cups_job_submit: failed to parse jobid from name %s\n",
1007 pjob->filename ));
1008 jobid = 0;
1011 if (!push_utf8_talloc(frame, &jobname, pjob->jobname, &size)) {
1012 goto out;
1014 new_jobname = talloc_asprintf(frame,
1015 "%s%.8u %s", PRINT_SPOOL_PREFIX,
1016 (unsigned int)jobid,
1017 jobname);
1018 if (new_jobname == NULL) {
1019 goto out;
1022 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name", NULL,
1023 new_jobname);
1026 * add any options defined in smb.conf
1029 if (!push_utf8_talloc(frame, &cupsoptions, lp_cups_options(snum), &size)) {
1030 goto out;
1032 num_options = 0;
1033 options = NULL;
1034 num_options = cupsParseOptions(cupsoptions, num_options, &options);
1036 if ( num_options )
1037 cupsEncodeOptions(request, num_options, options);
1040 * Do the request and get back a response...
1043 slprintf(uri, sizeof(uri) - 1, "/printers/%s", printername);
1045 if (!push_utf8_talloc(frame, &filename, pjob->filename, &size)) {
1046 goto out;
1048 if ((response = cupsDoFileRequest(http, request, uri, pjob->filename)) != NULL) {
1049 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1050 DEBUG(0,("Unable to print file to %s - %s\n", PRINTERNAME(snum),
1051 ippErrorString(cupsLastError())));
1052 } else {
1053 ret = 0;
1054 attr_job_id = ippFindAttribute(response, "job-id", IPP_TAG_INTEGER);
1055 if(attr_job_id) {
1056 pjob->sysjob = attr_job_id->values[0].integer;
1057 DEBUG(5,("cups_job_submit: job-id %d\n", pjob->sysjob));
1058 } else {
1059 DEBUG(0,("Missing job-id attribute in IPP response"));
1062 } else {
1063 DEBUG(0,("Unable to print file to `%s' - %s\n", PRINTERNAME(snum),
1064 ippErrorString(cupsLastError())));
1067 if ( ret == 0 )
1068 unlink(pjob->filename);
1069 /* else print_job_end will do it for us */
1071 out:
1072 if (response)
1073 ippDelete(response);
1075 if (language)
1076 cupsLangFree(language);
1078 if (http)
1079 httpClose(http);
1081 TALLOC_FREE(frame);
1083 return ret;
1087 * 'cups_queue_get()' - Get all the jobs in the print queue.
1090 static int cups_queue_get(const char *sharename,
1091 enum printing_types printing_type,
1092 char *lpq_command,
1093 print_queue_struct **q,
1094 print_status_struct *status)
1096 TALLOC_CTX *frame = talloc_stackframe();
1097 char *printername = NULL;
1098 http_t *http = NULL; /* HTTP connection to server */
1099 ipp_t *request = NULL, /* IPP Request */
1100 *response = NULL; /* IPP Response */
1101 ipp_attribute_t *attr = NULL; /* Current attribute */
1102 cups_lang_t *language = NULL; /* Default language */
1103 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
1104 int qcount = 0, /* Number of active queue entries */
1105 qalloc = 0; /* Number of queue entries allocated */
1106 print_queue_struct *queue = NULL, /* Queue entries */
1107 *temp; /* Temporary pointer for queue */
1108 char *user_name = NULL, /* job-originating-user-name attribute */
1109 *job_name = NULL; /* job-name attribute */
1110 int job_id; /* job-id attribute */
1111 int job_k_octets; /* job-k-octets attribute */
1112 time_t job_time; /* time-at-creation attribute */
1113 ipp_jstate_t job_status; /* job-status attribute */
1114 int job_priority; /* job-priority attribute */
1115 size_t size;
1116 static const char *jattrs[] = /* Requested job attributes */
1118 "job-id",
1119 "job-k-octets",
1120 "job-name",
1121 "job-originating-user-name",
1122 "job-priority",
1123 "job-state",
1124 "time-at-creation",
1126 static const char *pattrs[] = /* Requested printer attributes */
1128 "printer-state",
1129 "printer-state-message"
1132 *q = NULL;
1134 /* HACK ALERT!!! The problem with support the 'printer name'
1135 option is that we key the tdb off the sharename. So we will
1136 overload the lpq_command string to pass in the printername
1137 (which is basically what we do for non-cups printers ... using
1138 the lpq_command to get the queue listing). */
1140 if (!push_utf8_talloc(frame, &printername, lpq_command, &size)) {
1141 goto out;
1143 DEBUG(5,("cups_queue_get(%s, %p, %p)\n", lpq_command, q, status));
1146 * Make sure we don't ask for passwords...
1149 cupsSetPasswordCB(cups_passwd_cb);
1152 * Try to connect to the server...
1155 if ((http = cups_connect(frame)) == NULL) {
1156 goto out;
1160 * Generate the printer URI...
1163 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s", printername);
1166 * Build an IPP_GET_JOBS request, which requires the following
1167 * attributes:
1169 * attributes-charset
1170 * attributes-natural-language
1171 * requested-attributes
1172 * printer-uri
1175 request = ippNew();
1177 request->request.op.operation_id = IPP_GET_JOBS;
1178 request->request.op.request_id = 1;
1180 language = cupsLangDefault();
1182 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1183 "attributes-charset", NULL, "utf-8");
1185 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1186 "attributes-natural-language", NULL, language->language);
1188 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1189 "requested-attributes",
1190 (sizeof(jattrs) / sizeof(jattrs[0])),
1191 NULL, jattrs);
1193 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1194 "printer-uri", NULL, uri);
1197 * Do the request and get back a response...
1200 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
1201 DEBUG(0,("Unable to get jobs for %s - %s\n", uri,
1202 ippErrorString(cupsLastError())));
1203 goto out;
1206 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1207 DEBUG(0,("Unable to get jobs for %s - %s\n", uri,
1208 ippErrorString(response->request.status.status_code)));
1209 goto out;
1213 * Process the jobs...
1216 qcount = 0;
1217 qalloc = 0;
1218 queue = NULL;
1220 for (attr = response->attrs; attr != NULL; attr = attr->next) {
1222 * Skip leading attributes until we hit a job...
1225 while (attr != NULL && attr->group_tag != IPP_TAG_JOB)
1226 attr = attr->next;
1228 if (attr == NULL)
1229 break;
1232 * Allocate memory as needed...
1234 if (qcount >= qalloc) {
1235 qalloc += 16;
1237 queue = SMB_REALLOC_ARRAY(queue, print_queue_struct, qalloc);
1239 if (queue == NULL) {
1240 DEBUG(0,("cups_queue_get: Not enough memory!"));
1241 qcount = 0;
1242 goto out;
1246 temp = queue + qcount;
1247 memset(temp, 0, sizeof(print_queue_struct));
1250 * Pull the needed attributes from this job...
1253 job_id = 0;
1254 job_priority = 50;
1255 job_status = IPP_JOB_PENDING;
1256 job_time = 0;
1257 job_k_octets = 0;
1258 user_name = NULL;
1259 job_name = NULL;
1261 while (attr != NULL && attr->group_tag == IPP_TAG_JOB) {
1262 if (attr->name == NULL) {
1263 attr = attr->next;
1264 break;
1267 if (strcmp(attr->name, "job-id") == 0 &&
1268 attr->value_tag == IPP_TAG_INTEGER)
1269 job_id = attr->values[0].integer;
1271 if (strcmp(attr->name, "job-k-octets") == 0 &&
1272 attr->value_tag == IPP_TAG_INTEGER)
1273 job_k_octets = attr->values[0].integer;
1275 if (strcmp(attr->name, "job-priority") == 0 &&
1276 attr->value_tag == IPP_TAG_INTEGER)
1277 job_priority = attr->values[0].integer;
1279 if (strcmp(attr->name, "job-state") == 0 &&
1280 attr->value_tag == IPP_TAG_ENUM)
1281 job_status = (ipp_jstate_t)(attr->values[0].integer);
1283 if (strcmp(attr->name, "time-at-creation") == 0 &&
1284 attr->value_tag == IPP_TAG_INTEGER)
1285 job_time = attr->values[0].integer;
1287 if (strcmp(attr->name, "job-name") == 0 &&
1288 attr->value_tag == IPP_TAG_NAME) {
1289 if (!pull_utf8_talloc(frame,
1290 &job_name,
1291 attr->values[0].string.text,
1292 &size)) {
1293 goto out;
1297 if (strcmp(attr->name, "job-originating-user-name") == 0 &&
1298 attr->value_tag == IPP_TAG_NAME) {
1299 if (!pull_utf8_talloc(frame,
1300 &user_name,
1301 attr->values[0].string.text,
1302 &size)) {
1303 goto out;
1307 attr = attr->next;
1311 * See if we have everything needed...
1314 if (user_name == NULL || job_name == NULL || job_id == 0) {
1315 if (attr == NULL)
1316 break;
1317 else
1318 continue;
1321 temp->job = job_id;
1322 temp->size = job_k_octets * 1024;
1323 temp->status = job_status == IPP_JOB_PENDING ? LPQ_QUEUED :
1324 job_status == IPP_JOB_STOPPED ? LPQ_PAUSED :
1325 job_status == IPP_JOB_HELD ? LPQ_PAUSED :
1326 LPQ_PRINTING;
1327 temp->priority = job_priority;
1328 temp->time = job_time;
1329 strlcpy(temp->fs_user, user_name, sizeof(temp->fs_user));
1330 strlcpy(temp->fs_file, job_name, sizeof(temp->fs_file));
1332 qcount ++;
1334 if (attr == NULL)
1335 break;
1338 ippDelete(response);
1339 response = NULL;
1342 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the
1343 * following attributes:
1345 * attributes-charset
1346 * attributes-natural-language
1347 * requested-attributes
1348 * printer-uri
1351 request = ippNew();
1353 request->request.op.operation_id = IPP_GET_PRINTER_ATTRIBUTES;
1354 request->request.op.request_id = 1;
1356 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1357 "attributes-charset", NULL, "utf-8");
1359 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1360 "attributes-natural-language", NULL, language->language);
1362 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1363 "requested-attributes",
1364 (sizeof(pattrs) / sizeof(pattrs[0])),
1365 NULL, pattrs);
1367 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1368 "printer-uri", NULL, uri);
1371 * Do the request and get back a response...
1374 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
1375 DEBUG(0,("Unable to get printer status for %s - %s\n", printername,
1376 ippErrorString(cupsLastError())));
1377 goto out;
1380 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1381 DEBUG(0,("Unable to get printer status for %s - %s\n", printername,
1382 ippErrorString(response->request.status.status_code)));
1383 goto out;
1387 * Get the current printer status and convert it to the SAMBA values.
1390 if ((attr = ippFindAttribute(response, "printer-state", IPP_TAG_ENUM)) != NULL) {
1391 if (attr->values[0].integer == IPP_PRINTER_STOPPED)
1392 status->status = LPSTAT_STOPPED;
1393 else
1394 status->status = LPSTAT_OK;
1397 if ((attr = ippFindAttribute(response, "printer-state-message",
1398 IPP_TAG_TEXT)) != NULL) {
1399 char *msg = NULL;
1400 if (!pull_utf8_talloc(frame, &msg,
1401 attr->values[0].string.text,
1402 &size)) {
1403 SAFE_FREE(queue);
1404 qcount = 0;
1405 goto out;
1407 fstrcpy(status->message, msg);
1410 out:
1413 * Return the job queue...
1416 *q = queue;
1418 if (response)
1419 ippDelete(response);
1421 if (language)
1422 cupsLangFree(language);
1424 if (http)
1425 httpClose(http);
1427 TALLOC_FREE(frame);
1428 return qcount;
1433 * 'cups_queue_pause()' - Pause a print queue.
1436 static int cups_queue_pause(int snum)
1438 TALLOC_CTX *frame = talloc_stackframe();
1439 int ret = 1; /* Return value */
1440 http_t *http = NULL; /* HTTP connection to server */
1441 ipp_t *request = NULL, /* IPP Request */
1442 *response = NULL; /* IPP Response */
1443 cups_lang_t *language = NULL; /* Default language */
1444 char *printername = NULL;
1445 char *username = NULL;
1446 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
1447 size_t size;
1449 DEBUG(5,("cups_queue_pause(%d)\n", snum));
1452 * Make sure we don't ask for passwords...
1455 cupsSetPasswordCB(cups_passwd_cb);
1458 * Try to connect to the server...
1461 if ((http = cups_connect(frame)) == NULL) {
1462 goto out;
1466 * Build an IPP_PAUSE_PRINTER request, which requires the following
1467 * attributes:
1469 * attributes-charset
1470 * attributes-natural-language
1471 * printer-uri
1472 * requesting-user-name
1475 request = ippNew();
1477 request->request.op.operation_id = IPP_PAUSE_PRINTER;
1478 request->request.op.request_id = 1;
1480 language = cupsLangDefault();
1482 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1483 "attributes-charset", NULL, "utf-8");
1485 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1486 "attributes-natural-language", NULL, language->language);
1488 if (!push_utf8_talloc(frame, &printername, PRINTERNAME(snum), &size)) {
1489 goto out;
1491 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s",
1492 printername);
1494 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
1496 if (!push_utf8_talloc(frame, &username, current_user_info.unix_name, &size)) {
1497 goto out;
1499 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
1500 NULL, username);
1503 * Do the request and get back a response...
1506 if ((response = cupsDoRequest(http, request, "/admin/")) != NULL) {
1507 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1508 DEBUG(0,("Unable to pause printer %s - %s\n", PRINTERNAME(snum),
1509 ippErrorString(cupsLastError())));
1510 } else {
1511 ret = 0;
1513 } else {
1514 DEBUG(0,("Unable to pause printer %s - %s\n", PRINTERNAME(snum),
1515 ippErrorString(cupsLastError())));
1518 out:
1519 if (response)
1520 ippDelete(response);
1522 if (language)
1523 cupsLangFree(language);
1525 if (http)
1526 httpClose(http);
1528 TALLOC_FREE(frame);
1529 return ret;
1534 * 'cups_queue_resume()' - Restart a print queue.
1537 static int cups_queue_resume(int snum)
1539 TALLOC_CTX *frame = talloc_stackframe();
1540 int ret = 1; /* Return value */
1541 http_t *http = NULL; /* HTTP connection to server */
1542 ipp_t *request = NULL, /* IPP Request */
1543 *response = NULL; /* IPP Response */
1544 cups_lang_t *language = NULL; /* Default language */
1545 char *printername = NULL;
1546 char *username = NULL;
1547 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
1548 size_t size;
1550 DEBUG(5,("cups_queue_resume(%d)\n", snum));
1553 * Make sure we don't ask for passwords...
1556 cupsSetPasswordCB(cups_passwd_cb);
1559 * Try to connect to the server...
1562 if ((http = cups_connect(frame)) == NULL) {
1563 goto out;
1567 * Build an IPP_RESUME_PRINTER request, which requires the following
1568 * attributes:
1570 * attributes-charset
1571 * attributes-natural-language
1572 * printer-uri
1573 * requesting-user-name
1576 request = ippNew();
1578 request->request.op.operation_id = IPP_RESUME_PRINTER;
1579 request->request.op.request_id = 1;
1581 language = cupsLangDefault();
1583 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1584 "attributes-charset", NULL, "utf-8");
1586 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1587 "attributes-natural-language", NULL, language->language);
1589 if (!push_utf8_talloc(frame, &printername, PRINTERNAME(snum), &size)) {
1590 goto out;
1592 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s",
1593 printername);
1595 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
1597 if (!push_utf8_talloc(frame, &username, current_user_info.unix_name, &size)) {
1598 goto out;
1600 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
1601 NULL, username);
1604 * Do the request and get back a response...
1607 if ((response = cupsDoRequest(http, request, "/admin/")) != NULL) {
1608 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1609 DEBUG(0,("Unable to resume printer %s - %s\n", PRINTERNAME(snum),
1610 ippErrorString(cupsLastError())));
1611 } else {
1612 ret = 0;
1614 } else {
1615 DEBUG(0,("Unable to resume printer %s - %s\n", PRINTERNAME(snum),
1616 ippErrorString(cupsLastError())));
1619 out:
1620 if (response)
1621 ippDelete(response);
1623 if (language)
1624 cupsLangFree(language);
1626 if (http)
1627 httpClose(http);
1629 TALLOC_FREE(frame);
1630 return ret;
1633 /*******************************************************************
1634 * CUPS printing interface definitions...
1635 ******************************************************************/
1637 struct printif cups_printif =
1639 PRINT_CUPS,
1640 cups_queue_get,
1641 cups_queue_pause,
1642 cups_queue_resume,
1643 cups_job_delete,
1644 cups_job_pause,
1645 cups_job_resume,
1646 cups_job_submit,
1649 bool cups_pull_comment_location(NT_PRINTER_INFO_LEVEL_2 *printer)
1651 TALLOC_CTX *frame = talloc_stackframe();
1652 http_t *http = NULL; /* HTTP connection to server */
1653 ipp_t *request = NULL, /* IPP Request */
1654 *response = NULL; /* IPP Response */
1655 ipp_attribute_t *attr; /* Current attribute */
1656 cups_lang_t *language = NULL; /* Default language */
1657 char uri[HTTP_MAX_URI];
1658 char *server = NULL;
1659 char *sharename = NULL;
1660 char *name = NULL;
1661 static const char *requested[] =/* Requested attributes */
1663 "printer-name",
1664 "printer-info",
1665 "printer-location"
1667 bool ret = False;
1668 size_t size;
1670 DEBUG(5, ("pulling %s location\n", printer->sharename));
1673 * Make sure we don't ask for passwords...
1676 cupsSetPasswordCB(cups_passwd_cb);
1679 * Try to connect to the server...
1682 if ((http = cups_connect(frame)) == NULL) {
1683 goto out;
1686 request = ippNew();
1688 request->request.op.operation_id = IPP_GET_PRINTER_ATTRIBUTES;
1689 request->request.op.request_id = 1;
1691 language = cupsLangDefault();
1693 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1694 "attributes-charset", NULL, "utf-8");
1696 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1697 "attributes-natural-language", NULL, language->language);
1699 if (lp_cups_server() != NULL && strlen(lp_cups_server()) > 0) {
1700 if (!push_utf8_talloc(frame, &server, lp_cups_server(), &size)) {
1701 goto out;
1703 } else {
1704 server = talloc_strdup(frame,cupsServer());
1706 if (server) {
1707 goto out;
1709 if (!push_utf8_talloc(frame, &sharename, printer->sharename, &size)) {
1710 goto out;
1712 slprintf(uri, sizeof(uri) - 1, "ipp://%s/printers/%s",
1713 server, sharename);
1715 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1716 "printer-uri", NULL, uri);
1718 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1719 "requested-attributes",
1720 (sizeof(requested) / sizeof(requested[0])),
1721 NULL, requested);
1724 * Do the request and get back a response...
1727 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
1728 DEBUG(0,("Unable to get printer attributes - %s\n",
1729 ippErrorString(cupsLastError())));
1730 goto out;
1733 for (attr = response->attrs; attr != NULL;) {
1735 * Skip leading attributes until we hit a printer...
1738 while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)
1739 attr = attr->next;
1741 if (attr == NULL)
1742 break;
1745 * Pull the needed attributes from this printer...
1748 while ( attr && (attr->group_tag == IPP_TAG_PRINTER) ) {
1749 if (strcmp(attr->name, "printer-name") == 0 &&
1750 attr->value_tag == IPP_TAG_NAME) {
1751 if (!pull_utf8_talloc(frame,
1752 &name,
1753 attr->values[0].string.text,
1754 &size)) {
1755 goto out;
1759 /* Grab the comment if we don't have one */
1760 if ( (strcmp(attr->name, "printer-info") == 0)
1761 && (attr->value_tag == IPP_TAG_TEXT)
1762 && !strlen(printer->comment) )
1764 char *comment = NULL;
1765 if (!pull_utf8_talloc(frame,
1766 &comment,
1767 attr->values[0].string.text,
1768 &size)) {
1769 goto out;
1771 DEBUG(5,("cups_pull_comment_location: Using cups comment: %s\n",
1772 comment));
1773 strlcpy(printer->comment,
1774 comment,
1775 sizeof(printer->comment));
1778 /* Grab the location if we don't have one */
1779 if ( (strcmp(attr->name, "printer-location") == 0)
1780 && (attr->value_tag == IPP_TAG_TEXT)
1781 && !strlen(printer->location) )
1783 char *location = NULL;
1784 if (!pull_utf8_talloc(frame,
1785 &location,
1786 attr->values[0].string.text,
1787 &size)) {
1788 goto out;
1790 DEBUG(5,("cups_pull_comment_location: Using cups location: %s\n",
1791 location));
1792 strlcpy(printer->location,
1793 location,
1794 sizeof(printer->location));
1797 attr = attr->next;
1801 * We have everything needed...
1804 if (name != NULL)
1805 break;
1808 ret = True;
1810 out:
1811 if (response)
1812 ippDelete(response);
1814 if (request) {
1815 ippDelete(request);
1818 if (language)
1819 cupsLangFree(language);
1821 if (http)
1822 httpClose(http);
1824 TALLOC_FREE(frame);
1825 return ret;
1828 #else
1829 /* this keeps fussy compilers happy */
1830 void print_cups_dummy(void);
1831 void print_cups_dummy(void) {}
1832 #endif /* HAVE_CUPS */