source: vendor/3.5.2/source3/printing/print_cups.c

Last change on this file was 427, checked in by Silvan Scherrer, 16 years ago

Samba 3.5.x: update to 3.5.2

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