source: branches/samba-3.2.x/source/printing/print_cups.c

Last change on this file was 232, checked in by Herwig Bauernfeind, 16 years ago

Update 3.2 branch to 3.2.8

File size: 42.5 KB
Line 
1/*
2 * Support code for the Common UNIX Printing System ("CUPS")
3 *
4 * Copyright 1999-2003 by Michael R Sweet.
5 * Copyright 2008 Jeremy Allison.
6 *
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.
11 *
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.
16 *
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/>.
19 */
20
21/*
22 * JRA. Converted to utf8 pull/push.
23 */
24
25#include "includes.h"
26#include "printing.h"
27
28#ifdef HAVE_CUPS
29#include <cups/cups.h>
30#include <cups/language.h>
31
32extern userdom_struct current_user_info;
33
34/*
35 * 'cups_passwd_cb()' - The CUPS password callback...
36 */
37
38static const char * /* O - Password or NULL */
39cups_passwd_cb(const char *prompt) /* I - Prompt */
40{
41 /*
42 * Always return NULL to indicate that no password is available...
43 */
44
45 return (NULL);
46}
47
48static http_t *cups_connect(TALLOC_CTX *frame)
49{
50 http_t *http = NULL;
51 char *server = NULL, *p = NULL;
52 int port;
53
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;
57 }
58 } else {
59 server = talloc_strdup(frame,cupsServer());
60 }
61 if (!server) {
62 return NULL;
63 }
64
65 p = strchr(server, ':');
66 if (p) {
67 port = atoi(p+1);
68 *p = '\0';
69 } else {
70 port = ippPort();
71 }
72
73 DEBUG(10, ("connecting to cups server %s:%d\n",
74 server, port));
75
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;
80 }
81
82 return http;
83}
84
85static void send_pcap_info(const char *name, const char *info, void *pd)
86{
87 int fd = *(int *)pd;
88 size_t namelen = name ? strlen(name)+1 : 0;
89 size_t infolen = info ? strlen(info)+1 : 0;
90
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;
96 }
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;
102 }
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;
109 }
110 }
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;
117 }
118 }
119}
120
121static bool cups_cache_reload_async(int fd)
122{
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 */
133 {
134 "printer-name",
135 "printer-info"
136 };
137 bool ret = False;
138
139 DEBUG(5, ("reloading cups printcap cache\n"));
140
141 /*
142 * Make sure we don't ask for passwords...
143 */
144
145 cupsSetPasswordCB(cups_passwd_cb);
146
147 /*
148 * Try to connect to the server...
149 */
150
151 if ((http = cups_connect(frame)) == NULL) {
152 goto out;
153 }
154
155 /*
156 * Build a CUPS_GET_PRINTERS request, which requires the following
157 * attributes:
158 *
159 * attributes-charset
160 * attributes-natural-language
161 * requested-attributes
162 */
163
164 request = ippNew();
165
166 request->request.op.operation_id = CUPS_GET_PRINTERS;
167 request->request.op.request_id = 1;
168
169 language = cupsLangDefault();
170
171 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
172 "attributes-charset", NULL, "utf-8");
173
174 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
175 "attributes-natural-language", NULL, language->language);
176
177 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
178 "requested-attributes",
179 (sizeof(requested) / sizeof(requested[0])),
180 NULL, requested);
181
182 /*
183 * Do the request and get back a response...
184 */
185
186 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
187 DEBUG(0,("Unable to get printer list - %s\n",
188 ippErrorString(cupsLastError())));
189 goto out;
190 }
191
192 for (attr = response->attrs; attr != NULL;) {
193 /*
194 * Skip leading attributes until we hit a printer...
195 */
196
197 while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)
198 attr = attr->next;
199
200 if (attr == NULL)
201 break;
202
203 /*
204 * Pull the needed attributes from this printer...
205 */
206
207 name = NULL;
208 info = NULL;
209
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);
216 }
217
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);
223 }
224
225 attr = attr->next;
226 }
227
228 /*
229 * See if we have everything needed...
230 */
231
232 if (name == NULL)
233 break;
234
235 if (!pcap_cache_add_specific(&tmp_pcap_cache, name, info)) {
236 goto out;
237 }
238 }
239
240 ippDelete(response);
241 response = NULL;
242
243 /*
244 * Build a CUPS_GET_CLASSES request, which requires the following
245 * attributes:
246 *
247 * attributes-charset
248 * attributes-natural-language
249 * requested-attributes
250 */
251
252 request = ippNew();
253
254 request->request.op.operation_id = CUPS_GET_CLASSES;
255 request->request.op.request_id = 1;
256
257 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
258 "attributes-charset", NULL, "utf-8");
259
260 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
261 "attributes-natural-language", NULL, language->language);
262
263 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
264 "requested-attributes",
265 (sizeof(requested) / sizeof(requested[0])),
266 NULL, requested);
267
268 /*
269 * Do the request and get back a response...
270 */
271
272 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
273 DEBUG(0,("Unable to get printer list - %s\n",
274 ippErrorString(cupsLastError())));
275 goto out;
276 }
277
278 for (attr = response->attrs; attr != NULL;) {
279 /*
280 * Skip leading attributes until we hit a printer...
281 */
282
283 while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)
284 attr = attr->next;
285
286 if (attr == NULL)
287 break;
288
289 /*
290 * Pull the needed attributes from this printer...
291 */
292
293 name = NULL;
294 info = NULL;
295
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);
302 }
303
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);
309 }
310
311 attr = attr->next;
312 }
313
314 /*
315 * See if we have everything needed...
316 */
317
318 if (name == NULL)
319 break;
320
321 if (!pcap_cache_add_specific(&tmp_pcap_cache, name, info)) {
322 goto out;
323 }
324 }
325
326 ret = True;
327
328 out:
329 if (response)
330 ippDelete(response);
331
332 if (language)
333 cupsLangFree(language);
334
335 if (http)
336 httpClose(http);
337
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);
343
344 pcap_cache_destroy_specific(&tmp_pcap_cache);
345 }
346 TALLOC_FREE(frame);
347 return ret;
348}
349
350static struct pcap_cache *local_pcap_copy;
351struct fd_event *cache_fd_event;
352
353static bool cups_pcap_load_async(int *pfd)
354{
355 int fds[2];
356 pid_t pid;
357
358 *pfd = -1;
359
360 if (cache_fd_event) {
361 DEBUG(3,("cups_pcap_load_async: already waiting for "
362 "a refresh event\n" ));
363 return false;
364 }
365
366 DEBUG(5,("cups_pcap_load_async: asynchronously loading cups printers\n"));
367
368 if (pipe(fds) == -1) {
369 return false;
370 }
371
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;
379 }
380
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;
388 }
389
390 /* Child. */
391 close_all_print_db();
392
393 if (!reinit_after_fork(smbd_messaging_context(),
394 smbd_event_context(), true)) {
395 DEBUG(0,("cups_pcap_load_async: reinit_after_fork() failed\n"));
396 smb_panic("cups_pcap_load_async: reinit_after_fork() failed");
397 }
398
399 close(fds[0]);
400 cups_cache_reload_async(fds[1]);
401 close(fds[1]);
402 _exit(0);
403}
404
405static void cups_async_callback(struct event_context *event_ctx,
406 struct fd_event *event,
407 uint16 flags,
408 void *p)
409{
410 TALLOC_CTX *frame = talloc_stackframe();
411 int fd = *(int *)p;
412 struct pcap_cache *tmp_pcap_cache = NULL;
413
414 DEBUG(5,("cups_async_callback: callback received for printer data. "
415 "fd = %d\n", fd));
416
417 while (1) {
418 char *name = NULL, *info = NULL;
419 size_t namelen = 0, infolen = 0;
420 ssize_t ret = -1;
421
422 ret = sys_read(fd, &namelen, sizeof(namelen));
423 if (ret == 0) {
424 /* EOF */
425 break;
426 }
427 if (ret != sizeof(namelen)) {
428 DEBUG(10,("cups_async_callback: namelen read failed %d %s\n",
429 errno, strerror(errno)));
430 break;
431 }
432
433 DEBUG(11,("cups_async_callback: read namelen %u\n",
434 (unsigned int)namelen));
435
436 ret = sys_read(fd, &infolen, sizeof(infolen));
437 if (ret == 0) {
438 /* EOF */
439 break;
440 }
441 if (ret != sizeof(infolen)) {
442 DEBUG(10,("cups_async_callback: infolen read failed %s\n",
443 strerror(errno)));
444 break;
445 }
446
447 DEBUG(11,("cups_async_callback: read infolen %u\n",
448 (unsigned int)infolen));
449
450 if (namelen) {
451 name = TALLOC_ARRAY(frame, char, namelen);
452 if (!name) {
453 break;
454 }
455 ret = sys_read(fd, name, namelen);
456 if (ret == 0) {
457 /* EOF */
458 break;
459 }
460 if (ret != namelen) {
461 DEBUG(10,("cups_async_callback: name read failed %s\n",
462 strerror(errno)));
463 break;
464 }
465 DEBUG(11,("cups_async_callback: read name %s\n",
466 name));
467 } else {
468 name = NULL;
469 }
470 if (infolen) {
471 info = TALLOC_ARRAY(frame, char, infolen);
472 if (!info) {
473 break;
474 }
475 ret = sys_read(fd, info, infolen);
476 if (ret == 0) {
477 /* EOF */
478 break;
479 }
480 if (ret != infolen) {
481 DEBUG(10,("cups_async_callback: info read failed %s\n",
482 strerror(errno)));
483 break;
484 }
485 DEBUG(11,("cups_async_callback: read info %s\n",
486 info));
487 } else {
488 info = NULL;
489 }
490
491 /* Add to our local pcap cache. */
492 pcap_cache_add_specific(&tmp_pcap_cache, name, info);
493 TALLOC_FREE(name);
494 TALLOC_FREE(info);
495 }
496
497 TALLOC_FREE(frame);
498 if (tmp_pcap_cache) {
499 /* We got a namelist, replace our local cache. */
500 pcap_cache_destroy_specific(&local_pcap_copy);
501 local_pcap_copy = tmp_pcap_cache;
502
503 /* And the systemwide pcap cache. */
504 pcap_cache_replace(local_pcap_copy);
505 } else {
506 DEBUG(2,("cups_async_callback: failed to read a new "
507 "printer list\n"));
508 }
509 close(fd);
510 TALLOC_FREE(p);
511 TALLOC_FREE(cache_fd_event);
512}
513
514bool cups_cache_reload(void)
515{
516 int *p_pipe_fd = TALLOC_P(NULL, int);
517
518 if (!p_pipe_fd) {
519 return false;
520 }
521
522 *p_pipe_fd = -1;
523
524 /* Set up an async refresh. */
525 if (!cups_pcap_load_async(p_pipe_fd)) {
526 return false;
527 }
528 if (!local_pcap_copy) {
529 /* We have no local cache, wait directly for
530 * async refresh to complete.
531 */
532 DEBUG(10,("cups_cache_reload: sync read on fd %d\n",
533 *p_pipe_fd ));
534
535 cups_async_callback(smbd_event_context(),
536 NULL,
537 EVENT_FD_READ,
538 (void *)p_pipe_fd);
539 if (!local_pcap_copy) {
540 return false;
541 }
542 } else {
543 /* Replace the system cache with our
544 * local copy. */
545 pcap_cache_replace(local_pcap_copy);
546
547 DEBUG(10,("cups_cache_reload: async read on fd %d\n",
548 *p_pipe_fd ));
549
550 /* Trigger an event when the pipe can be read. */
551 cache_fd_event = event_add_fd(smbd_event_context(),
552 NULL, *p_pipe_fd,
553 EVENT_FD_READ,
554 cups_async_callback,
555 (void *)p_pipe_fd);
556 if (!cache_fd_event) {
557 close(*p_pipe_fd);
558 TALLOC_FREE(p_pipe_fd);
559 return false;
560 }
561 }
562 return true;
563}
564
565/*
566 * 'cups_job_delete()' - Delete a job.
567 */
568
569static int cups_job_delete(const char *sharename, const char *lprm_command, struct printjob *pjob)
570{
571 TALLOC_CTX *frame = talloc_stackframe();
572 int ret = 1; /* Return value */
573 http_t *http = NULL; /* HTTP connection to server */
574 ipp_t *request = NULL, /* IPP Request */
575 *response = NULL; /* IPP Response */
576 cups_lang_t *language = NULL; /* Default language */
577 char *user = NULL;
578 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
579
580
581 DEBUG(5,("cups_job_delete(%s, %p (%d))\n", sharename, pjob, pjob->sysjob));
582
583 /*
584 * Make sure we don't ask for passwords...
585 */
586
587 cupsSetPasswordCB(cups_passwd_cb);
588
589 /*
590 * Try to connect to the server...
591 */
592
593 if ((http = cups_connect(frame)) == NULL) {
594 goto out;
595 }
596
597 /*
598 * Build an IPP_CANCEL_JOB request, which requires the following
599 * attributes:
600 *
601 * attributes-charset
602 * attributes-natural-language
603 * job-uri
604 * requesting-user-name
605 */
606
607 request = ippNew();
608
609 request->request.op.operation_id = IPP_CANCEL_JOB;
610 request->request.op.request_id = 1;
611
612 language = cupsLangDefault();
613
614 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
615 "attributes-charset", NULL, "utf-8");
616
617 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
618 "attributes-natural-language", NULL, language->language);
619
620 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/jobs/%d", pjob->sysjob);
621
622 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri);
623
624 if (push_utf8_talloc(frame, &user, pjob->user) == (size_t)-1) {
625 goto out;
626 }
627
628 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
629 NULL, user);
630
631 /*
632 * Do the request and get back a response...
633 */
634
635 if ((response = cupsDoRequest(http, request, "/jobs")) != NULL) {
636 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
637 DEBUG(0,("Unable to cancel job %d - %s\n", pjob->sysjob,
638 ippErrorString(cupsLastError())));
639 } else {
640 ret = 0;
641 }
642 } else {
643 DEBUG(0,("Unable to cancel job %d - %s\n", pjob->sysjob,
644 ippErrorString(cupsLastError())));
645 }
646
647 out:
648 if (response)
649 ippDelete(response);
650
651 if (language)
652 cupsLangFree(language);
653
654 if (http)
655 httpClose(http);
656
657 TALLOC_FREE(frame);
658 return ret;
659}
660
661
662/*
663 * 'cups_job_pause()' - Pause a job.
664 */
665
666static int cups_job_pause(int snum, struct printjob *pjob)
667{
668 TALLOC_CTX *frame = talloc_stackframe();
669 int ret = 1; /* Return value */
670 http_t *http = NULL; /* HTTP connection to server */
671 ipp_t *request = NULL, /* IPP Request */
672 *response = NULL; /* IPP Response */
673 cups_lang_t *language = NULL; /* Default language */
674 char *user = NULL;
675 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
676
677
678 DEBUG(5,("cups_job_pause(%d, %p (%d))\n", snum, pjob, pjob->sysjob));
679
680 /*
681 * Make sure we don't ask for passwords...
682 */
683
684 cupsSetPasswordCB(cups_passwd_cb);
685
686 /*
687 * Try to connect to the server...
688 */
689
690 if ((http = cups_connect(frame)) == NULL) {
691 goto out;
692 }
693
694 /*
695 * Build an IPP_HOLD_JOB request, which requires the following
696 * attributes:
697 *
698 * attributes-charset
699 * attributes-natural-language
700 * job-uri
701 * requesting-user-name
702 */
703
704 request = ippNew();
705
706 request->request.op.operation_id = IPP_HOLD_JOB;
707 request->request.op.request_id = 1;
708
709 language = cupsLangDefault();
710
711 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
712 "attributes-charset", NULL, "utf-8");
713
714 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
715 "attributes-natural-language", NULL, language->language);
716
717 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/jobs/%d", pjob->sysjob);
718
719 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri);
720
721 if (push_utf8_talloc(frame, &user, pjob->user) == (size_t)-1) {
722 goto out;
723 }
724 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
725 NULL, user);
726
727 /*
728 * Do the request and get back a response...
729 */
730
731 if ((response = cupsDoRequest(http, request, "/jobs")) != NULL) {
732 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
733 DEBUG(0,("Unable to hold job %d - %s\n", pjob->sysjob,
734 ippErrorString(cupsLastError())));
735 } else {
736 ret = 0;
737 }
738 } else {
739 DEBUG(0,("Unable to hold job %d - %s\n", pjob->sysjob,
740 ippErrorString(cupsLastError())));
741 }
742
743 out:
744 if (response)
745 ippDelete(response);
746
747 if (language)
748 cupsLangFree(language);
749
750 if (http)
751 httpClose(http);
752
753 TALLOC_FREE(frame);
754 return ret;
755}
756
757
758/*
759 * 'cups_job_resume()' - Resume a paused job.
760 */
761
762static int cups_job_resume(int snum, struct printjob *pjob)
763{
764 TALLOC_CTX *frame = talloc_stackframe();
765 int ret = 1; /* Return value */
766 http_t *http = NULL; /* HTTP connection to server */
767 ipp_t *request = NULL, /* IPP Request */
768 *response = NULL; /* IPP Response */
769 cups_lang_t *language = NULL; /* Default language */
770 char *user = NULL;
771 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
772
773
774 DEBUG(5,("cups_job_resume(%d, %p (%d))\n", snum, pjob, pjob->sysjob));
775
776 /*
777 * Make sure we don't ask for passwords...
778 */
779
780 cupsSetPasswordCB(cups_passwd_cb);
781
782 /*
783 * Try to connect to the server...
784 */
785
786 if ((http = cups_connect(frame)) == NULL) {
787 goto out;
788 }
789
790 /*
791 * Build an IPP_RELEASE_JOB request, which requires the following
792 * attributes:
793 *
794 * attributes-charset
795 * attributes-natural-language
796 * job-uri
797 * requesting-user-name
798 */
799
800 request = ippNew();
801
802 request->request.op.operation_id = IPP_RELEASE_JOB;
803 request->request.op.request_id = 1;
804
805 language = cupsLangDefault();
806
807 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
808 "attributes-charset", NULL, "utf-8");
809
810 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
811 "attributes-natural-language", NULL, language->language);
812
813 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/jobs/%d", pjob->sysjob);
814
815 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri);
816
817 if (push_utf8_talloc(frame, &user, pjob->user) == (size_t)-1) {
818 goto out;
819 }
820 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
821 NULL, user);
822
823 /*
824 * Do the request and get back a response...
825 */
826
827 if ((response = cupsDoRequest(http, request, "/jobs")) != NULL) {
828 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
829 DEBUG(0,("Unable to release job %d - %s\n", pjob->sysjob,
830 ippErrorString(cupsLastError())));
831 } else {
832 ret = 0;
833 }
834 } else {
835 DEBUG(0,("Unable to release job %d - %s\n", pjob->sysjob,
836 ippErrorString(cupsLastError())));
837 }
838
839 out:
840 if (response)
841 ippDelete(response);
842
843 if (language)
844 cupsLangFree(language);
845
846 if (http)
847 httpClose(http);
848
849 TALLOC_FREE(frame);
850 return ret;
851}
852
853
854/*
855 * 'cups_job_submit()' - Submit a job for printing.
856 */
857
858static int cups_job_submit(int snum, struct printjob *pjob)
859{
860 TALLOC_CTX *frame = talloc_stackframe();
861 int ret = 1; /* Return value */
862 http_t *http = NULL; /* HTTP connection to server */
863 ipp_t *request = NULL, /* IPP Request */
864 *response = NULL; /* IPP Response */
865 cups_lang_t *language = NULL; /* Default language */
866 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
867 const char *clientname = NULL; /* hostname of client for job-originating-host attribute */
868 char *new_jobname = NULL;
869 int num_options = 0;
870 cups_option_t *options = NULL;
871 char *printername = NULL;
872 char *user = NULL;
873 char *jobname = NULL;
874 char *cupsoptions = NULL;
875 char *filename = NULL;
876 char addr[INET6_ADDRSTRLEN];
877
878 DEBUG(5,("cups_job_submit(%d, %p (%d))\n", snum, pjob, pjob->sysjob));
879
880 /*
881 * Make sure we don't ask for passwords...
882 */
883
884 cupsSetPasswordCB(cups_passwd_cb);
885
886 /*
887 * Try to connect to the server...
888 */
889
890 if ((http = cups_connect(frame)) == NULL) {
891 goto out;
892 }
893
894 /*
895 * Build an IPP_PRINT_JOB request, which requires the following
896 * attributes:
897 *
898 * attributes-charset
899 * attributes-natural-language
900 * printer-uri
901 * requesting-user-name
902 * [document-data]
903 */
904
905 request = ippNew();
906
907 request->request.op.operation_id = IPP_PRINT_JOB;
908 request->request.op.request_id = 1;
909
910 language = cupsLangDefault();
911
912 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
913 "attributes-charset", NULL, "utf-8");
914
915 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
916 "attributes-natural-language", NULL, language->language);
917
918 if (push_utf8_talloc(frame, &printername, PRINTERNAME(snum)) == (size_t)-1) {
919 goto out;
920 }
921 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s",
922 printername);
923
924 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
925 "printer-uri", NULL, uri);
926
927 if (push_utf8_talloc(frame, &user, pjob->user) == (size_t)-1) {
928 goto out;
929 }
930 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
931 NULL, user);
932
933 clientname = client_name(get_client_fd());
934 if (strcmp(clientname, "UNKNOWN") == 0) {
935 clientname = client_addr(get_client_fd(),addr,sizeof(addr));
936 }
937
938 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
939 "job-originating-host-name", NULL,
940 clientname);
941
942 if (push_utf8_talloc(frame, &jobname, pjob->jobname) == (size_t)-1) {
943 goto out;
944 }
945 new_jobname = talloc_asprintf(frame,
946 "%s%.8u %s", PRINT_SPOOL_PREFIX,
947 (unsigned int)pjob->smbjob,
948 jobname);
949 if (new_jobname == NULL) {
950 goto out;
951 }
952
953 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name", NULL,
954 new_jobname);
955
956 /*
957 * add any options defined in smb.conf
958 */
959
960 if (push_utf8_talloc(frame, &cupsoptions, lp_cups_options(snum)) == (size_t)-1) {
961 goto out;
962 }
963 num_options = 0;
964 options = NULL;
965 num_options = cupsParseOptions(cupsoptions, num_options, &options);
966
967 if ( num_options )
968 cupsEncodeOptions(request, num_options, options);
969
970 /*
971 * Do the request and get back a response...
972 */
973
974 slprintf(uri, sizeof(uri) - 1, "/printers/%s", printername);
975
976 if (push_utf8_talloc(frame, &filename, pjob->filename) == (size_t)-1) {
977 goto out;
978 }
979 if ((response = cupsDoFileRequest(http, request, uri, pjob->filename)) != NULL) {
980 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
981 DEBUG(0,("Unable to print file to %s - %s\n", PRINTERNAME(snum),
982 ippErrorString(cupsLastError())));
983 } else {
984 ret = 0;
985 }
986 } else {
987 DEBUG(0,("Unable to print file to `%s' - %s\n", PRINTERNAME(snum),
988 ippErrorString(cupsLastError())));
989 }
990
991 if ( ret == 0 )
992 unlink(pjob->filename);
993 /* else print_job_end will do it for us */
994
995 out:
996 if (response)
997 ippDelete(response);
998
999 if (language)
1000 cupsLangFree(language);
1001
1002 if (http)
1003 httpClose(http);
1004
1005 TALLOC_FREE(frame);
1006
1007 return ret;
1008}
1009
1010/*
1011 * 'cups_queue_get()' - Get all the jobs in the print queue.
1012 */
1013
1014static int cups_queue_get(const char *sharename,
1015 enum printing_types printing_type,
1016 char *lpq_command,
1017 print_queue_struct **q,
1018 print_status_struct *status)
1019{
1020 TALLOC_CTX *frame = talloc_stackframe();
1021 char *printername = NULL;
1022 http_t *http = NULL; /* HTTP connection to server */
1023 ipp_t *request = NULL, /* IPP Request */
1024 *response = NULL; /* IPP Response */
1025 ipp_attribute_t *attr = NULL; /* Current attribute */
1026 cups_lang_t *language = NULL; /* Default language */
1027 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
1028 int qcount = 0, /* Number of active queue entries */
1029 qalloc = 0; /* Number of queue entries allocated */
1030 print_queue_struct *queue = NULL, /* Queue entries */
1031 *temp; /* Temporary pointer for queue */
1032 char *user_name = NULL, /* job-originating-user-name attribute */
1033 *job_name = NULL; /* job-name attribute */
1034 int job_id; /* job-id attribute */
1035 int job_k_octets; /* job-k-octets attribute */
1036 time_t job_time; /* time-at-creation attribute */
1037 ipp_jstate_t job_status; /* job-status attribute */
1038 int job_priority; /* job-priority attribute */
1039 static const char *jattrs[] = /* Requested job attributes */
1040 {
1041 "job-id",
1042 "job-k-octets",
1043 "job-name",
1044 "job-originating-user-name",
1045 "job-priority",
1046 "job-state",
1047 "time-at-creation",
1048 };
1049 static const char *pattrs[] = /* Requested printer attributes */
1050 {
1051 "printer-state",
1052 "printer-state-message"
1053 };
1054
1055 *q = NULL;
1056
1057 /* HACK ALERT!!! The problem with support the 'printer name'
1058 option is that we key the tdb off the sharename. So we will
1059 overload the lpq_command string to pass in the printername
1060 (which is basically what we do for non-cups printers ... using
1061 the lpq_command to get the queue listing). */
1062
1063 if (push_utf8_talloc(frame, &printername, lpq_command) == (size_t)-1) {
1064 goto out;
1065 }
1066 DEBUG(5,("cups_queue_get(%s, %p, %p)\n", lpq_command, q, status));
1067
1068 /*
1069 * Make sure we don't ask for passwords...
1070 */
1071
1072 cupsSetPasswordCB(cups_passwd_cb);
1073
1074 /*
1075 * Try to connect to the server...
1076 */
1077
1078 if ((http = cups_connect(frame)) == NULL) {
1079 goto out;
1080 }
1081
1082 /*
1083 * Generate the printer URI...
1084 */
1085
1086 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s", printername);
1087
1088 /*
1089 * Build an IPP_GET_JOBS request, which requires the following
1090 * attributes:
1091 *
1092 * attributes-charset
1093 * attributes-natural-language
1094 * requested-attributes
1095 * printer-uri
1096 */
1097
1098 request = ippNew();
1099
1100 request->request.op.operation_id = IPP_GET_JOBS;
1101 request->request.op.request_id = 1;
1102
1103 language = cupsLangDefault();
1104
1105 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1106 "attributes-charset", NULL, "utf-8");
1107
1108 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1109 "attributes-natural-language", NULL, language->language);
1110
1111 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1112 "requested-attributes",
1113 (sizeof(jattrs) / sizeof(jattrs[0])),
1114 NULL, jattrs);
1115
1116 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1117 "printer-uri", NULL, uri);
1118
1119 /*
1120 * Do the request and get back a response...
1121 */
1122
1123 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
1124 DEBUG(0,("Unable to get jobs for %s - %s\n", uri,
1125 ippErrorString(cupsLastError())));
1126 goto out;
1127 }
1128
1129 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1130 DEBUG(0,("Unable to get jobs for %s - %s\n", uri,
1131 ippErrorString(response->request.status.status_code)));
1132 goto out;
1133 }
1134
1135 /*
1136 * Process the jobs...
1137 */
1138
1139 qcount = 0;
1140 qalloc = 0;
1141 queue = NULL;
1142
1143 for (attr = response->attrs; attr != NULL; attr = attr->next) {
1144 /*
1145 * Skip leading attributes until we hit a job...
1146 */
1147
1148 while (attr != NULL && attr->group_tag != IPP_TAG_JOB)
1149 attr = attr->next;
1150
1151 if (attr == NULL)
1152 break;
1153
1154 /*
1155 * Allocate memory as needed...
1156 */
1157 if (qcount >= qalloc) {
1158 qalloc += 16;
1159
1160 queue = SMB_REALLOC_ARRAY(queue, print_queue_struct, qalloc);
1161
1162 if (queue == NULL) {
1163 DEBUG(0,("cups_queue_get: Not enough memory!"));
1164 qcount = 0;
1165 goto out;
1166 }
1167 }
1168
1169 temp = queue + qcount;
1170 memset(temp, 0, sizeof(print_queue_struct));
1171
1172 /*
1173 * Pull the needed attributes from this job...
1174 */
1175
1176 job_id = 0;
1177 job_priority = 50;
1178 job_status = IPP_JOB_PENDING;
1179 job_time = 0;
1180 job_k_octets = 0;
1181 user_name = NULL;
1182 job_name = NULL;
1183
1184 while (attr != NULL && attr->group_tag == IPP_TAG_JOB) {
1185 if (attr->name == NULL) {
1186 attr = attr->next;
1187 break;
1188 }
1189
1190 if (strcmp(attr->name, "job-id") == 0 &&
1191 attr->value_tag == IPP_TAG_INTEGER)
1192 job_id = attr->values[0].integer;
1193
1194 if (strcmp(attr->name, "job-k-octets") == 0 &&
1195 attr->value_tag == IPP_TAG_INTEGER)
1196 job_k_octets = attr->values[0].integer;
1197
1198 if (strcmp(attr->name, "job-priority") == 0 &&
1199 attr->value_tag == IPP_TAG_INTEGER)
1200 job_priority = attr->values[0].integer;
1201
1202 if (strcmp(attr->name, "job-state") == 0 &&
1203 attr->value_tag == IPP_TAG_ENUM)
1204 job_status = (ipp_jstate_t)(attr->values[0].integer);
1205
1206 if (strcmp(attr->name, "time-at-creation") == 0 &&
1207 attr->value_tag == IPP_TAG_INTEGER)
1208 job_time = attr->values[0].integer;
1209
1210 if (strcmp(attr->name, "job-name") == 0 &&
1211 attr->value_tag == IPP_TAG_NAME) {
1212 pull_utf8_talloc(frame,
1213 &job_name,
1214 attr->values[0].string.text);
1215 }
1216
1217 if (strcmp(attr->name, "job-originating-user-name") == 0 &&
1218 attr->value_tag == IPP_TAG_NAME) {
1219 pull_utf8_talloc(frame,
1220 &user_name,
1221 attr->values[0].string.text);
1222 }
1223
1224 attr = attr->next;
1225 }
1226
1227 /*
1228 * See if we have everything needed...
1229 */
1230
1231 if (user_name == NULL || job_name == NULL || job_id == 0) {
1232 if (attr == NULL)
1233 break;
1234 else
1235 continue;
1236 }
1237
1238 temp->job = job_id;
1239 temp->size = job_k_octets * 1024;
1240 temp->status = job_status == IPP_JOB_PENDING ? LPQ_QUEUED :
1241 job_status == IPP_JOB_STOPPED ? LPQ_PAUSED :
1242 job_status == IPP_JOB_HELD ? LPQ_PAUSED :
1243 LPQ_PRINTING;
1244 temp->priority = job_priority;
1245 temp->time = job_time;
1246 strlcpy(temp->fs_user, user_name, sizeof(temp->fs_user));
1247 strlcpy(temp->fs_file, job_name, sizeof(temp->fs_file));
1248
1249 qcount ++;
1250
1251 if (attr == NULL)
1252 break;
1253 }
1254
1255 ippDelete(response);
1256 response = NULL;
1257
1258 /*
1259 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the
1260 * following attributes:
1261 *
1262 * attributes-charset
1263 * attributes-natural-language
1264 * requested-attributes
1265 * printer-uri
1266 */
1267
1268 request = ippNew();
1269
1270 request->request.op.operation_id = IPP_GET_PRINTER_ATTRIBUTES;
1271 request->request.op.request_id = 1;
1272
1273 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1274 "attributes-charset", NULL, "utf-8");
1275
1276 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1277 "attributes-natural-language", NULL, language->language);
1278
1279 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1280 "requested-attributes",
1281 (sizeof(pattrs) / sizeof(pattrs[0])),
1282 NULL, pattrs);
1283
1284 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1285 "printer-uri", NULL, uri);
1286
1287 /*
1288 * Do the request and get back a response...
1289 */
1290
1291 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
1292 DEBUG(0,("Unable to get printer status for %s - %s\n", printername,
1293 ippErrorString(cupsLastError())));
1294 *q = queue;
1295 goto out;
1296 }
1297
1298 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1299 DEBUG(0,("Unable to get printer status for %s - %s\n", printername,
1300 ippErrorString(response->request.status.status_code)));
1301 *q = queue;
1302 goto out;
1303 }
1304
1305 /*
1306 * Get the current printer status and convert it to the SAMBA values.
1307 */
1308
1309 if ((attr = ippFindAttribute(response, "printer-state", IPP_TAG_ENUM)) != NULL) {
1310 if (attr->values[0].integer == IPP_PRINTER_STOPPED)
1311 status->status = LPSTAT_STOPPED;
1312 else
1313 status->status = LPSTAT_OK;
1314 }
1315
1316 if ((attr = ippFindAttribute(response, "printer-state-message",
1317 IPP_TAG_TEXT)) != NULL) {
1318 char *msg = NULL;
1319 pull_utf8_talloc(frame, &msg, attr->values[0].string.text);
1320 fstrcpy(status->message, msg);
1321 }
1322
1323 /*
1324 * Return the job queue...
1325 */
1326
1327 *q = queue;
1328
1329 out:
1330 if (response)
1331 ippDelete(response);
1332
1333 if (language)
1334 cupsLangFree(language);
1335
1336 if (http)
1337 httpClose(http);
1338
1339 TALLOC_FREE(frame);
1340 return qcount;
1341}
1342
1343
1344/*
1345 * 'cups_queue_pause()' - Pause a print queue.
1346 */
1347
1348static int cups_queue_pause(int snum)
1349{
1350 TALLOC_CTX *frame = talloc_stackframe();
1351 int ret = 1; /* Return value */
1352 http_t *http = NULL; /* HTTP connection to server */
1353 ipp_t *request = NULL, /* IPP Request */
1354 *response = NULL; /* IPP Response */
1355 cups_lang_t *language = NULL; /* Default language */
1356 char *printername = NULL;
1357 char *username = NULL;
1358 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
1359
1360
1361 DEBUG(5,("cups_queue_pause(%d)\n", snum));
1362
1363 /*
1364 * Make sure we don't ask for passwords...
1365 */
1366
1367 cupsSetPasswordCB(cups_passwd_cb);
1368
1369 /*
1370 * Try to connect to the server...
1371 */
1372
1373 if ((http = cups_connect(frame)) == NULL) {
1374 goto out;
1375 }
1376
1377 /*
1378 * Build an IPP_PAUSE_PRINTER request, which requires the following
1379 * attributes:
1380 *
1381 * attributes-charset
1382 * attributes-natural-language
1383 * printer-uri
1384 * requesting-user-name
1385 */
1386
1387 request = ippNew();
1388
1389 request->request.op.operation_id = IPP_PAUSE_PRINTER;
1390 request->request.op.request_id = 1;
1391
1392 language = cupsLangDefault();
1393
1394 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1395 "attributes-charset", NULL, "utf-8");
1396
1397 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1398 "attributes-natural-language", NULL, language->language);
1399
1400 if (push_utf8_talloc(frame, &printername, PRINTERNAME(snum)) == (size_t)-1) {
1401 goto out;
1402 }
1403 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s",
1404 printername);
1405
1406 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
1407
1408 if (push_utf8_talloc(frame, &username, current_user_info.unix_name) == (size_t)-1) {
1409 goto out;
1410 }
1411 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
1412 NULL, username);
1413
1414 /*
1415 * Do the request and get back a response...
1416 */
1417
1418 if ((response = cupsDoRequest(http, request, "/admin/")) != NULL) {
1419 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1420 DEBUG(0,("Unable to pause printer %s - %s\n", PRINTERNAME(snum),
1421 ippErrorString(cupsLastError())));
1422 } else {
1423 ret = 0;
1424 }
1425 } else {
1426 DEBUG(0,("Unable to pause printer %s - %s\n", PRINTERNAME(snum),
1427 ippErrorString(cupsLastError())));
1428 }
1429
1430 out:
1431 if (response)
1432 ippDelete(response);
1433
1434 if (language)
1435 cupsLangFree(language);
1436
1437 if (http)
1438 httpClose(http);
1439
1440 TALLOC_FREE(frame);
1441 return ret;
1442}
1443
1444
1445/*
1446 * 'cups_queue_resume()' - Restart a print queue.
1447 */
1448
1449static int cups_queue_resume(int snum)
1450{
1451 TALLOC_CTX *frame = talloc_stackframe();
1452 int ret = 1; /* Return value */
1453 http_t *http = NULL; /* HTTP connection to server */
1454 ipp_t *request = NULL, /* IPP Request */
1455 *response = NULL; /* IPP Response */
1456 cups_lang_t *language = NULL; /* Default language */
1457 char *printername = NULL;
1458 char *username = NULL;
1459 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
1460
1461
1462 DEBUG(5,("cups_queue_resume(%d)\n", snum));
1463
1464 /*
1465 * Make sure we don't ask for passwords...
1466 */
1467
1468 cupsSetPasswordCB(cups_passwd_cb);
1469
1470 /*
1471 * Try to connect to the server...
1472 */
1473
1474 if ((http = cups_connect(frame)) == NULL) {
1475 goto out;
1476 }
1477
1478 /*
1479 * Build an IPP_RESUME_PRINTER request, which requires the following
1480 * attributes:
1481 *
1482 * attributes-charset
1483 * attributes-natural-language
1484 * printer-uri
1485 * requesting-user-name
1486 */
1487
1488 request = ippNew();
1489
1490 request->request.op.operation_id = IPP_RESUME_PRINTER;
1491 request->request.op.request_id = 1;
1492
1493 language = cupsLangDefault();
1494
1495 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1496 "attributes-charset", NULL, "utf-8");
1497
1498 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1499 "attributes-natural-language", NULL, language->language);
1500
1501 if (push_utf8_talloc(frame, &printername, PRINTERNAME(snum)) == (size_t)-1) {
1502 goto out;
1503 }
1504 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s",
1505 printername);
1506
1507 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
1508
1509 if (push_utf8_talloc(frame, &username, current_user_info.unix_name) == (size_t)-1) {
1510 goto out;
1511 }
1512 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
1513 NULL, username);
1514
1515 /*
1516 * Do the request and get back a response...
1517 */
1518
1519 if ((response = cupsDoRequest(http, request, "/admin/")) != NULL) {
1520 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1521 DEBUG(0,("Unable to resume printer %s - %s\n", PRINTERNAME(snum),
1522 ippErrorString(cupsLastError())));
1523 } else {
1524 ret = 0;
1525 }
1526 } else {
1527 DEBUG(0,("Unable to resume printer %s - %s\n", PRINTERNAME(snum),
1528 ippErrorString(cupsLastError())));
1529 }
1530
1531 out:
1532 if (response)
1533 ippDelete(response);
1534
1535 if (language)
1536 cupsLangFree(language);
1537
1538 if (http)
1539 httpClose(http);
1540
1541 TALLOC_FREE(frame);
1542 return ret;
1543}
1544
1545/*******************************************************************
1546 * CUPS printing interface definitions...
1547 ******************************************************************/
1548
1549struct printif cups_printif =
1550{
1551 PRINT_CUPS,
1552 cups_queue_get,
1553 cups_queue_pause,
1554 cups_queue_resume,
1555 cups_job_delete,
1556 cups_job_pause,
1557 cups_job_resume,
1558 cups_job_submit,
1559};
1560
1561bool cups_pull_comment_location(NT_PRINTER_INFO_LEVEL_2 *printer)
1562{
1563 TALLOC_CTX *frame = talloc_stackframe();
1564 http_t *http = NULL; /* HTTP connection to server */
1565 ipp_t *request = NULL, /* IPP Request */
1566 *response = NULL; /* IPP Response */
1567 ipp_attribute_t *attr; /* Current attribute */
1568 cups_lang_t *language = NULL; /* Default language */
1569 char uri[HTTP_MAX_URI];
1570 char *server = NULL;
1571 char *sharename = NULL;
1572 char *name = NULL;
1573 static const char *requested[] =/* Requested attributes */
1574 {
1575 "printer-name",
1576 "printer-info",
1577 "printer-location"
1578 };
1579 bool ret = False;
1580
1581 DEBUG(5, ("pulling %s location\n", printer->sharename));
1582
1583 /*
1584 * Make sure we don't ask for passwords...
1585 */
1586
1587 cupsSetPasswordCB(cups_passwd_cb);
1588
1589 /*
1590 * Try to connect to the server...
1591 */
1592
1593 if ((http = cups_connect(frame)) == NULL) {
1594 goto out;
1595 }
1596
1597 request = ippNew();
1598
1599 request->request.op.operation_id = IPP_GET_PRINTER_ATTRIBUTES;
1600 request->request.op.request_id = 1;
1601
1602 language = cupsLangDefault();
1603
1604 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1605 "attributes-charset", NULL, "utf-8");
1606
1607 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1608 "attributes-natural-language", NULL, language->language);
1609
1610 if (lp_cups_server() != NULL && strlen(lp_cups_server()) > 0) {
1611 if (push_utf8_talloc(frame, &server, lp_cups_server()) == (size_t)-1) {
1612 goto out;
1613 }
1614 } else {
1615 server = talloc_strdup(frame,cupsServer());
1616 }
1617 if (server) {
1618 goto out;
1619 }
1620 if (push_utf8_talloc(frame, &sharename, printer->sharename) == (size_t)-1) {
1621 goto out;
1622 }
1623 slprintf(uri, sizeof(uri) - 1, "ipp://%s/printers/%s",
1624 server, sharename);
1625
1626 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1627 "printer-uri", NULL, uri);
1628
1629 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1630 "requested-attributes",
1631 (sizeof(requested) / sizeof(requested[0])),
1632 NULL, requested);
1633
1634 /*
1635 * Do the request and get back a response...
1636 */
1637
1638 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
1639 DEBUG(0,("Unable to get printer attributes - %s\n",
1640 ippErrorString(cupsLastError())));
1641 goto out;
1642 }
1643
1644 for (attr = response->attrs; attr != NULL;) {
1645 /*
1646 * Skip leading attributes until we hit a printer...
1647 */
1648
1649 while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)
1650 attr = attr->next;
1651
1652 if (attr == NULL)
1653 break;
1654
1655 /*
1656 * Pull the needed attributes from this printer...
1657 */
1658
1659 while ( attr && (attr->group_tag == IPP_TAG_PRINTER) ) {
1660 if (strcmp(attr->name, "printer-name") == 0 &&
1661 attr->value_tag == IPP_TAG_NAME) {
1662 pull_utf8_talloc(frame,
1663 &name,
1664 attr->values[0].string.text);
1665 }
1666
1667 /* Grab the comment if we don't have one */
1668 if ( (strcmp(attr->name, "printer-info") == 0)
1669 && (attr->value_tag == IPP_TAG_TEXT)
1670 && !strlen(printer->comment) )
1671 {
1672 char *comment = NULL;
1673 pull_utf8_talloc(frame,
1674 &comment,
1675 attr->values[0].string.text);
1676 DEBUG(5,("cups_pull_comment_location: Using cups comment: %s\n",
1677 comment));
1678 strlcpy(printer->comment,
1679 comment,
1680 sizeof(printer->comment));
1681 }
1682
1683 /* Grab the location if we don't have one */
1684 if ( (strcmp(attr->name, "printer-location") == 0)
1685 && (attr->value_tag == IPP_TAG_TEXT)
1686 && !strlen(printer->location) )
1687 {
1688 char *location = NULL;
1689 pull_utf8_talloc(frame,
1690 &location,
1691 attr->values[0].string.text);
1692 DEBUG(5,("cups_pull_comment_location: Using cups location: %s\n",
1693 location));
1694 strlcpy(printer->location,
1695 location,
1696 sizeof(printer->location));
1697 }
1698
1699 attr = attr->next;
1700 }
1701
1702 /*
1703 * We have everything needed...
1704 */
1705
1706 if (name != NULL)
1707 break;
1708 }
1709
1710 ret = True;
1711
1712 out:
1713 if (response)
1714 ippDelete(response);
1715
1716 if (request) {
1717 ippDelete(request);
1718 }
1719
1720 if (language)
1721 cupsLangFree(language);
1722
1723 if (http)
1724 httpClose(http);
1725
1726 TALLOC_FREE(frame);
1727 return ret;
1728}
1729
1730#else
1731 /* this keeps fussy compilers happy */
1732 void print_cups_dummy(void);
1733 void print_cups_dummy(void) {}
1734#endif /* HAVE_CUPS */
Note: See TracBrowser for help on using the repository browser.