35e3b30dd8ee8fd747adbdb003e836fd4b12fb6e
[ddiss/samba.git] / source3 / printing / print_cups.c
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 #include "printing/pcap.h"
28 #include "librpc/gen_ndr/ndr_printcap.h"
29
30 #ifdef HAVE_CUPS
31 #include <cups/cups.h>
32 #include <cups/language.h>
33
34 static SIG_ATOMIC_T gotalarm;
35
36 /***************************************************************
37  Signal function to tell us we timed out.
38 ****************************************************************/
39
40 static void gotalarm_sig(int signum)
41 {
42         gotalarm = 1;
43 }
44
45 extern userdom_struct current_user_info;
46
47 /*
48  * 'cups_passwd_cb()' - The CUPS password callback...
49  */
50
51 static const char *                             /* O - Password or NULL */
52 cups_passwd_cb(const char *prompt)      /* I - Prompt */
53 {
54         /*
55          * Always return NULL to indicate that no password is available...
56          */
57
58         return (NULL);
59 }
60
61 static http_t *cups_connect(TALLOC_CTX *frame)
62 {
63         http_t *http = NULL;
64         char *server = NULL, *p = NULL;
65         int port;
66         int timeout = lp_cups_connection_timeout();
67         size_t size;
68
69         if (lp_cups_server() != NULL && strlen(lp_cups_server()) > 0) {
70                 if (!push_utf8_talloc(frame, &server, lp_cups_server(), &size)) {
71                         return NULL;
72                 }
73         } else {
74                 server = talloc_strdup(frame,cupsServer());
75         }
76         if (!server) {
77                 return NULL;
78         }
79
80         p = strchr(server, ':');
81         if (p) {
82                 port = atoi(p+1);
83                 *p = '\0';
84         } else {
85                 port = ippPort();
86         }
87
88         DEBUG(10, ("connecting to cups server %s:%d\n",
89                    server, port));
90
91         gotalarm = 0;
92
93         if (timeout) {
94                 CatchSignal(SIGALRM, gotalarm_sig);
95                 alarm(timeout);
96         }
97
98 #ifdef HAVE_HTTPCONNECTENCRYPT
99         http = httpConnectEncrypt(server, port, lp_cups_encrypt());
100 #else
101         http = httpConnect(server, port);
102 #endif
103
104
105         CatchSignal(SIGALRM, SIG_IGN);
106         alarm(0);
107
108         if (http == NULL) {
109                 DEBUG(0,("Unable to connect to CUPS server %s:%d - %s\n",
110                          server, port, strerror(errno)));
111         }
112
113         return http;
114 }
115
116 static bool send_pcap_blob(DATA_BLOB *pcap_blob, int fd)
117 {
118         size_t ret;
119
120         ret = sys_write(fd, &pcap_blob->length, sizeof(pcap_blob->length));
121         if (ret != sizeof(pcap_blob->length)) {
122                 return false;
123         }
124
125         ret = sys_write(fd, pcap_blob->data, pcap_blob->length);
126         if (ret != pcap_blob->length) {
127                 return false;
128         }
129
130         DEBUG(10, ("successfully sent blob of len %ld\n", (int64_t)ret));
131         return true;
132 }
133
134 static bool recv_pcap_blob(TALLOC_CTX *mem_ctx, int fd, DATA_BLOB *pcap_blob)
135 {
136         size_t blob_len;
137         size_t ret;
138
139         ret = sys_read(fd, &blob_len, sizeof(blob_len));
140         if (ret != sizeof(blob_len)) {
141                 return false;
142         }
143
144         *pcap_blob = data_blob_talloc_named(mem_ctx, NULL, blob_len,
145                                            "cups pcap");
146         if (pcap_blob->length != blob_len) {
147                 return false;
148         }
149         ret = sys_read(fd, pcap_blob->data, blob_len);
150         if (ret != blob_len) {
151                 talloc_free(pcap_blob->data);
152                 return false;
153         }
154
155         DEBUG(10, ("successfully recvd blob of len %ld\n", (int64_t)ret));
156         return true;
157 }
158
159 static bool cups_cache_reload_async(int fd)
160 {
161         TALLOC_CTX *frame = talloc_stackframe();
162         struct pcap_data pcap_data;
163         struct pcap_printer *printer;
164         http_t          *http = NULL;           /* HTTP connection to server */
165         ipp_t           *request = NULL,        /* IPP Request */
166                         *response = NULL;       /* IPP Response */
167         ipp_attribute_t *attr;          /* Current attribute */
168         cups_lang_t     *language = NULL;       /* Default language */
169         char            *name,          /* printer-name attribute */
170                         *info;          /* printer-info attribute */
171         static const char *requested[] =/* Requested attributes */
172                         {
173                           "printer-name",
174                           "printer-info"
175                         };
176         bool ret = False;
177         size_t size;
178         enum ndr_err_code ndr_ret;
179         DATA_BLOB pcap_blob;
180
181         ZERO_STRUCT(pcap_data);
182         pcap_data.status = NT_STATUS_UNSUCCESSFUL;
183
184         DEBUG(5, ("reloading cups printcap cache\n"));
185
186        /*
187         * Make sure we don't ask for passwords...
188         */
189
190         cupsSetPasswordCB(cups_passwd_cb);
191
192        /*
193         * Try to connect to the server...
194         */
195
196         if ((http = cups_connect(frame)) == NULL) {
197                 goto out;
198         }
199
200        /*
201         * Build a CUPS_GET_PRINTERS request, which requires the following
202         * attributes:
203         *
204         *    attributes-charset
205         *    attributes-natural-language
206         *    requested-attributes
207         */
208
209         request = ippNew();
210
211         request->request.op.operation_id = CUPS_GET_PRINTERS;
212         request->request.op.request_id   = 1;
213
214         language = cupsLangDefault();
215
216         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
217                      "attributes-charset", NULL, "utf-8");
218
219         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
220                      "attributes-natural-language", NULL, language->language);
221
222         ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
223                       "requested-attributes",
224                       (sizeof(requested) / sizeof(requested[0])),
225                       NULL, requested);
226
227        /*
228         * Do the request and get back a response...
229         */
230
231         if ((response = cupsDoRequest(http, request, "/")) == NULL) {
232                 DEBUG(0,("Unable to get printer list - %s\n",
233                          ippErrorString(cupsLastError())));
234                 goto out;
235         }
236
237         for (attr = response->attrs; attr != NULL;) {
238                /*
239                 * Skip leading attributes until we hit a printer...
240                 */
241
242                 while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)
243                         attr = attr->next;
244
245                 if (attr == NULL)
246                         break;
247
248                /*
249                 * Pull the needed attributes from this printer...
250                 */
251
252                 name       = NULL;
253                 info       = NULL;
254
255                 while (attr != NULL && attr->group_tag == IPP_TAG_PRINTER) {
256                         if (strcmp(attr->name, "printer-name") == 0 &&
257                             attr->value_tag == IPP_TAG_NAME) {
258                                 if (!pull_utf8_talloc(frame,
259                                                 &name,
260                                                 attr->values[0].string.text,
261                                                 &size)) {
262                                         goto out;
263                                 }
264                         }
265
266                         if (strcmp(attr->name, "printer-info") == 0 &&
267                             attr->value_tag == IPP_TAG_TEXT) {
268                                 if (!pull_utf8_talloc(frame,
269                                                 &info,
270                                                 attr->values[0].string.text,
271                                                 &size)) {
272                                         goto out;
273                                 }
274                         }
275
276                         attr = attr->next;
277                 }
278
279                /*
280                 * See if we have everything needed...
281                 */
282
283                 if (name == NULL)
284                         break;
285
286                 if (pcap_data.count == 0) {
287                         printer = talloc_array(frame, struct pcap_printer, 1);
288                 } else {
289                         printer = talloc_realloc(frame, pcap_data.printers,
290                                                  struct pcap_printer,
291                                                  pcap_data.count + 1);
292                 }
293                 if (printer == NULL) {
294                         goto out;
295                 }
296                 pcap_data.printers = printer;
297                 pcap_data.printers[pcap_data.count].name = name;
298                 pcap_data.printers[pcap_data.count].info = info;
299                 pcap_data.count++;
300         }
301
302         ippDelete(response);
303         response = NULL;
304
305        /*
306         * Build a CUPS_GET_CLASSES request, which requires the following
307         * attributes:
308         *
309         *    attributes-charset
310         *    attributes-natural-language
311         *    requested-attributes
312         */
313
314         request = ippNew();
315
316         request->request.op.operation_id = CUPS_GET_CLASSES;
317         request->request.op.request_id   = 1;
318
319         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
320                      "attributes-charset", NULL, "utf-8");
321
322         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
323                      "attributes-natural-language", NULL, language->language);
324
325         ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
326                       "requested-attributes",
327                       (sizeof(requested) / sizeof(requested[0])),
328                       NULL, requested);
329
330        /*
331         * Do the request and get back a response...
332         */
333
334         if ((response = cupsDoRequest(http, request, "/")) == NULL) {
335                 DEBUG(0,("Unable to get printer list - %s\n",
336                          ippErrorString(cupsLastError())));
337                 goto out;
338         }
339
340         for (attr = response->attrs; attr != NULL;) {
341                /*
342                 * Skip leading attributes until we hit a printer...
343                 */
344
345                 while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)
346                         attr = attr->next;
347
348                 if (attr == NULL)
349                         break;
350
351                /*
352                 * Pull the needed attributes from this printer...
353                 */
354
355                 name       = NULL;
356                 info       = NULL;
357
358                 while (attr != NULL && attr->group_tag == IPP_TAG_PRINTER) {
359                         if (strcmp(attr->name, "printer-name") == 0 &&
360                             attr->value_tag == IPP_TAG_NAME) {
361                                 if (!pull_utf8_talloc(frame,
362                                                 &name,
363                                                 attr->values[0].string.text,
364                                                 &size)) {
365                                         goto out;
366                                 }
367                         }
368
369                         if (strcmp(attr->name, "printer-info") == 0 &&
370                             attr->value_tag == IPP_TAG_TEXT) {
371                                 if (!pull_utf8_talloc(frame,
372                                                 &info,
373                                                 attr->values[0].string.text,
374                                                 &size)) {
375                                         goto out;
376                                 }
377                         }
378
379                         attr = attr->next;
380                 }
381
382                /*
383                 * See if we have everything needed...
384                 */
385
386                 if (name == NULL)
387                         break;
388
389                 if (pcap_data.count == 0) {
390                         printer = talloc_array(frame, struct pcap_printer, 1);
391                 } else {
392                         printer = talloc_realloc(frame, pcap_data.printers,
393                                                  struct pcap_printer,
394                                                  pcap_data.count + 1);
395                 }
396                 if (printer == NULL) {
397                         goto out;
398                 }
399                 pcap_data.printers = printer;
400                 pcap_data.printers[pcap_data.count].name = name;
401                 pcap_data.printers[pcap_data.count].info = info;
402                 pcap_data.count++;
403         }
404
405         ret = true;
406         pcap_data.status = NT_STATUS_OK;
407  out:
408         if (response)
409                 ippDelete(response);
410
411         if (language)
412                 cupsLangFree(language);
413
414         if (http)
415                 httpClose(http);
416
417         /* Send all the entries up the pipe. */
418         ndr_ret = ndr_push_struct_blob(&pcap_blob, frame, &pcap_data,
419                                        (ndr_push_flags_fn_t)ndr_push_pcap_data);
420         if (ndr_ret == NDR_ERR_SUCCESS) {
421                 ret = send_pcap_blob(&pcap_blob, fd);
422         } else {
423                 ret = false;
424         }
425
426         TALLOC_FREE(frame);
427         return ret;
428 }
429
430 static struct fd_event *cache_fd_event;
431
432 static bool cups_pcap_load_async(struct tevent_context *ev,
433                                  struct messaging_context *msg_ctx,
434                                  int *pfd)
435 {
436         int fds[2];
437         pid_t pid;
438         NTSTATUS status;
439
440         *pfd = -1;
441
442         if (cache_fd_event) {
443                 DEBUG(3,("cups_pcap_load_async: already waiting for "
444                         "a refresh event\n" ));
445                 return false;
446         }
447
448         DEBUG(5,("cups_pcap_load_async: asynchronously loading cups printers\n"));
449
450         if (pipe(fds) == -1) {
451                 return false;
452         }
453
454         pid = sys_fork();
455         if (pid == (pid_t)-1) {
456                 DEBUG(10,("cups_pcap_load_async: fork failed %s\n",
457                         strerror(errno) ));
458                 close(fds[0]);
459                 close(fds[1]);
460                 return false;
461         }
462
463         if (pid) {
464                 DEBUG(10,("cups_pcap_load_async: child pid = %u\n",
465                         (unsigned int)pid ));
466                 /* Parent. */
467                 close(fds[1]);
468                 *pfd = fds[0];
469                 return true;
470         }
471
472         /* Child. */
473
474         close_all_print_db();
475
476         status = reinit_after_fork(msg_ctx, ev, procid_self(), true);
477         if (!NT_STATUS_IS_OK(status)) {
478                 DEBUG(0,("cups_pcap_load_async: reinit_after_fork() failed\n"));
479                 smb_panic("cups_pcap_load_async: reinit_after_fork() failed");
480         }
481
482         close(fds[0]);
483         cups_cache_reload_async(fds[1]);
484         close(fds[1]);
485         _exit(0);
486 }
487
488 struct cups_async_cb_args {
489         int pipe_fd;
490         struct event_context *event_ctx;
491         struct messaging_context *msg_ctx;
492         void (*post_cache_fill_fn)(struct event_context *,
493                                    struct messaging_context *);
494 };
495
496 static void cups_async_callback(struct event_context *event_ctx,
497                                 struct fd_event *event,
498                                 uint16 flags,
499                                 void *p)
500 {
501         TALLOC_CTX *frame = talloc_stackframe();
502         struct cups_async_cb_args *cb_args = (struct cups_async_cb_args *)p;
503         struct pcap_cache *tmp_pcap_cache = NULL;
504         bool ret_ok;
505         struct pcap_data pcap_data;
506         DATA_BLOB pcap_blob;
507         enum ndr_err_code ndr_ret;
508         int i;
509
510         DEBUG(5,("cups_async_callback: callback received for printer data. "
511                 "fd = %d\n", cb_args->pipe_fd));
512
513         ret_ok = recv_pcap_blob(frame, cb_args->pipe_fd, &pcap_blob);
514         if (!ret_ok) {
515                 DEBUG(0,("failed to recv pcap blob\n"));
516                 goto err_out;
517         }
518
519         ndr_ret = ndr_pull_struct_blob(&pcap_blob, frame, &pcap_data,
520                                        (ndr_pull_flags_fn_t)ndr_pull_pcap_data);
521         if (ndr_ret != NDR_ERR_SUCCESS) {
522                 goto err_out;
523         }
524
525         if (!NT_STATUS_IS_OK(pcap_data.status)) {
526                 DEBUG(0,("failed to retrieve printer list: %s\n",
527                          nt_errstr(pcap_data.status)));
528                 goto err_out;
529         }
530
531         for (i = 0; i < pcap_data.count; i++) {
532                 ret_ok = pcap_cache_add_specific(&tmp_pcap_cache,
533                                                  pcap_data.printers[i].name,
534                                                  pcap_data.printers[i].info);
535                 if (!ret_ok) {
536                         DEBUG(0, ("failed to add to tmp pcap cache\n"));
537                         break;
538                 }
539         }
540
541         if (!ret_ok) {
542                 DEBUG(0, ("failed to read a new printer list\n"));
543                 pcap_cache_destroy_specific(&tmp_pcap_cache);
544         } else {
545                 /*
546                  * replace the system-wide pcap cache with a (possibly empty)
547                  * new one.
548                  */
549                 ret_ok = pcap_cache_replace(tmp_pcap_cache);
550                 if (!ret_ok) {
551                         DEBUG(0, ("failed to replace pcap cache\n"));
552                 } else if (cb_args->post_cache_fill_fn != NULL) {
553                         /* Caller requested post cache fill callback */
554                         cb_args->post_cache_fill_fn(cb_args->event_ctx,
555                                                     cb_args->msg_ctx);
556                 }
557         }
558 err_out:
559         TALLOC_FREE(frame);
560         close(cb_args->pipe_fd);
561         TALLOC_FREE(cb_args);
562         TALLOC_FREE(cache_fd_event);
563 }
564
565 bool cups_cache_reload(struct tevent_context *ev,
566                        struct messaging_context *msg_ctx,
567                        void (*post_cache_fill_fn)(struct tevent_context *,
568                                                   struct messaging_context *))
569 {
570         struct cups_async_cb_args *cb_args;
571         int *p_pipe_fd;
572
573         cb_args = TALLOC_P(NULL, struct cups_async_cb_args);
574         if (cb_args == NULL) {
575                 return false;
576         }
577
578         cb_args->post_cache_fill_fn = post_cache_fill_fn;
579         cb_args->event_ctx = ev;
580         cb_args->msg_ctx = msg_ctx;
581         p_pipe_fd = &cb_args->pipe_fd;
582         *p_pipe_fd = -1;
583
584         /* Set up an async refresh. */
585         if (!cups_pcap_load_async(ev, msg_ctx, p_pipe_fd)) {
586                 talloc_free(cb_args);
587                 return false;
588         }
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(ev,
595                                 NULL, *p_pipe_fd,
596                                 EVENT_FD_READ,
597                                 cups_async_callback,
598                                 (void *)cb_args);
599         if (!cache_fd_event) {
600                 close(*p_pipe_fd);
601                 TALLOC_FREE(cb_args);
602                 return false;
603         }
604
605         return true;
606 }
607
608 /*
609  * 'cups_job_delete()' - Delete a job.
610  */
611
612 static 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
709 static 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
805 static 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
901 static 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         char *new_jobname = NULL;
912         int             num_options = 0;
913         cups_option_t   *options = NULL;
914         char *printername = NULL;
915         char *user = NULL;
916         char *jobname = NULL;
917         char *cupsoptions = NULL;
918         char *filename = NULL;
919         size_t size;
920         uint32_t jobid = (uint32_t)-1;
921
922         DEBUG(5,("cups_job_submit(%d, %p)\n", snum, pjob));
923
924        /*
925         * Make sure we don't ask for passwords...
926         */
927
928         cupsSetPasswordCB(cups_passwd_cb);
929
930        /*
931         * Try to connect to the server...
932         */
933
934         if ((http = cups_connect(frame)) == NULL) {
935                 goto out;
936         }
937
938        /*
939         * Build an IPP_PRINT_JOB request, which requires the following
940         * attributes:
941         *
942         *    attributes-charset
943         *    attributes-natural-language
944         *    printer-uri
945         *    requesting-user-name
946         *    [document-data]
947         */
948
949         request = ippNew();
950
951         request->request.op.operation_id = IPP_PRINT_JOB;
952         request->request.op.request_id   = 1;
953
954         language = cupsLangDefault();
955
956         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
957                      "attributes-charset", NULL, "utf-8");
958
959         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
960                      "attributes-natural-language", NULL, language->language);
961
962         if (!push_utf8_talloc(frame, &printername, lp_printername(snum),
963                               &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         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
979                      "job-originating-host-name", NULL,
980                      pjob->clientmachine);
981
982         /* Get the jobid from the filename. */
983         jobid = print_parse_jobid(pjob->filename);
984         if (jobid == (uint32_t)-1) {
985                 DEBUG(0,("cups_job_submit: failed to parse jobid from name %s\n",
986                                 pjob->filename ));
987                 jobid = 0;
988         }
989
990         if (!push_utf8_talloc(frame, &jobname, pjob->jobname, &size)) {
991                 goto out;
992         }
993         new_jobname = talloc_asprintf(frame,
994                         "%s%.8u %s", PRINT_SPOOL_PREFIX,
995                         (unsigned int)jobid,
996                         jobname);
997         if (new_jobname == NULL) {
998                 goto out;
999         }
1000
1001         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name", NULL,
1002                      new_jobname);
1003
1004         /*
1005          * add any options defined in smb.conf
1006          */
1007
1008         if (!push_utf8_talloc(frame, &cupsoptions, lp_cups_options(snum), &size)) {
1009                 goto out;
1010         }
1011         num_options = 0;
1012         options     = NULL;
1013         num_options = cupsParseOptions(cupsoptions, num_options, &options);
1014
1015         if ( num_options )
1016                 cupsEncodeOptions(request, num_options, options);
1017
1018        /*
1019         * Do the request and get back a response...
1020         */
1021
1022         slprintf(uri, sizeof(uri) - 1, "/printers/%s", printername);
1023
1024         if (!push_utf8_talloc(frame, &filename, pjob->filename, &size)) {
1025                 goto out;
1026         }
1027         if ((response = cupsDoFileRequest(http, request, uri, pjob->filename)) != NULL) {
1028                 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1029                         DEBUG(0,("Unable to print file to %s - %s\n",
1030                                  lp_printername(snum),
1031                                  ippErrorString(cupsLastError())));
1032                 } else {
1033                         ret = 0;
1034                         attr_job_id = ippFindAttribute(response, "job-id", IPP_TAG_INTEGER);
1035                         if(attr_job_id) {
1036                                 pjob->sysjob = attr_job_id->values[0].integer;
1037                                 DEBUG(5,("cups_job_submit: job-id %d\n", pjob->sysjob));
1038                         } else {
1039                                 DEBUG(0,("Missing job-id attribute in IPP response"));
1040                         }
1041                 }
1042         } else {
1043                 DEBUG(0,("Unable to print file to `%s' - %s\n",
1044                          lp_printername(snum),
1045                          ippErrorString(cupsLastError())));
1046         }
1047
1048         if ( ret == 0 )
1049                 unlink(pjob->filename);
1050         /* else print_job_end will do it for us */
1051
1052  out:
1053         if (response)
1054                 ippDelete(response);
1055
1056         if (language)
1057                 cupsLangFree(language);
1058
1059         if (http)
1060                 httpClose(http);
1061
1062         TALLOC_FREE(frame);
1063
1064         return ret;
1065 }
1066
1067 /*
1068  * 'cups_queue_get()' - Get all the jobs in the print queue.
1069  */
1070
1071 static int cups_queue_get(const char *sharename,
1072                enum printing_types printing_type,
1073                char *lpq_command,
1074                print_queue_struct **q,
1075                print_status_struct *status)
1076 {
1077         TALLOC_CTX *frame = talloc_stackframe();
1078         char *printername = NULL;
1079         http_t          *http = NULL;           /* HTTP connection to server */
1080         ipp_t           *request = NULL,        /* IPP Request */
1081                         *response = NULL;       /* IPP Response */
1082         ipp_attribute_t *attr = NULL;           /* Current attribute */
1083         cups_lang_t     *language = NULL;       /* Default language */
1084         char            uri[HTTP_MAX_URI]; /* printer-uri attribute */
1085         int             qcount = 0,             /* Number of active queue entries */
1086                         qalloc = 0;             /* Number of queue entries allocated */
1087         print_queue_struct *queue = NULL,       /* Queue entries */
1088                         *temp;          /* Temporary pointer for queue */
1089         char            *user_name = NULL,      /* job-originating-user-name attribute */
1090                         *job_name = NULL;       /* job-name attribute */
1091         int             job_id;         /* job-id attribute */
1092         int             job_k_octets;   /* job-k-octets attribute */
1093         time_t          job_time;       /* time-at-creation attribute */
1094         ipp_jstate_t    job_status;     /* job-status attribute */
1095         int             job_priority;   /* job-priority attribute */
1096         size_t size;
1097         static const char *jattrs[] =   /* Requested job attributes */
1098                         {
1099                           "job-id",
1100                           "job-k-octets",
1101                           "job-name",
1102                           "job-originating-user-name",
1103                           "job-priority",
1104                           "job-state",
1105                           "time-at-creation",
1106                         };
1107         static const char *pattrs[] =   /* Requested printer attributes */
1108                         {
1109                           "printer-state",
1110                           "printer-state-message"
1111                         };
1112
1113         *q = NULL;
1114
1115         /* HACK ALERT!!!  The problem with support the 'printer name'
1116            option is that we key the tdb off the sharename.  So we will
1117            overload the lpq_command string to pass in the printername
1118            (which is basically what we do for non-cups printers ... using
1119            the lpq_command to get the queue listing). */
1120
1121         if (!push_utf8_talloc(frame, &printername, lpq_command, &size)) {
1122                 goto out;
1123         }
1124         DEBUG(5,("cups_queue_get(%s, %p, %p)\n", lpq_command, q, status));
1125
1126        /*
1127         * Make sure we don't ask for passwords...
1128         */
1129
1130         cupsSetPasswordCB(cups_passwd_cb);
1131
1132        /*
1133         * Try to connect to the server...
1134         */
1135
1136         if ((http = cups_connect(frame)) == NULL) {
1137                 goto out;
1138         }
1139
1140        /*
1141         * Generate the printer URI...
1142         */
1143
1144         slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s", printername);
1145
1146        /*
1147         * Build an IPP_GET_JOBS request, which requires the following
1148         * attributes:
1149         *
1150         *    attributes-charset
1151         *    attributes-natural-language
1152         *    requested-attributes
1153         *    printer-uri
1154         */
1155
1156         request = ippNew();
1157
1158         request->request.op.operation_id = IPP_GET_JOBS;
1159         request->request.op.request_id   = 1;
1160
1161         language = cupsLangDefault();
1162
1163         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1164                      "attributes-charset", NULL, "utf-8");
1165
1166         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1167                      "attributes-natural-language", NULL, language->language);
1168
1169         ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1170                       "requested-attributes",
1171                       (sizeof(jattrs) / sizeof(jattrs[0])),
1172                       NULL, jattrs);
1173
1174         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1175                      "printer-uri", NULL, uri);
1176
1177        /*
1178         * Do the request and get back a response...
1179         */
1180
1181         if ((response = cupsDoRequest(http, request, "/")) == NULL) {
1182                 DEBUG(0,("Unable to get jobs for %s - %s\n", uri,
1183                          ippErrorString(cupsLastError())));
1184                 goto out;
1185         }
1186
1187         if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1188                 DEBUG(0,("Unable to get jobs for %s - %s\n", uri,
1189                          ippErrorString(response->request.status.status_code)));
1190                 goto out;
1191         }
1192
1193        /*
1194         * Process the jobs...
1195         */
1196
1197         qcount = 0;
1198         qalloc = 0;
1199         queue  = NULL;
1200
1201         for (attr = response->attrs; attr != NULL; attr = attr->next) {
1202                /*
1203                 * Skip leading attributes until we hit a job...
1204                 */
1205
1206                 while (attr != NULL && attr->group_tag != IPP_TAG_JOB)
1207                         attr = attr->next;
1208
1209                 if (attr == NULL)
1210                         break;
1211
1212                /*
1213                 * Allocate memory as needed...
1214                 */
1215                 if (qcount >= qalloc) {
1216                         qalloc += 16;
1217
1218                         queue = SMB_REALLOC_ARRAY(queue, print_queue_struct, qalloc);
1219
1220                         if (queue == NULL) {
1221                                 DEBUG(0,("cups_queue_get: Not enough memory!"));
1222                                 qcount = 0;
1223                                 goto out;
1224                         }
1225                 }
1226
1227                 temp = queue + qcount;
1228                 memset(temp, 0, sizeof(print_queue_struct));
1229
1230                /*
1231                 * Pull the needed attributes from this job...
1232                 */
1233
1234                 job_id       = 0;
1235                 job_priority = 50;
1236                 job_status   = IPP_JOB_PENDING;
1237                 job_time     = 0;
1238                 job_k_octets = 0;
1239                 user_name    = NULL;
1240                 job_name     = NULL;
1241
1242                 while (attr != NULL && attr->group_tag == IPP_TAG_JOB) {
1243                         if (attr->name == NULL) {
1244                                 attr = attr->next;
1245                                 break;
1246                         }
1247
1248                         if (strcmp(attr->name, "job-id") == 0 &&
1249                             attr->value_tag == IPP_TAG_INTEGER)
1250                                 job_id = attr->values[0].integer;
1251
1252                         if (strcmp(attr->name, "job-k-octets") == 0 &&
1253                             attr->value_tag == IPP_TAG_INTEGER)
1254                                 job_k_octets = attr->values[0].integer;
1255
1256                         if (strcmp(attr->name, "job-priority") == 0 &&
1257                             attr->value_tag == IPP_TAG_INTEGER)
1258                                 job_priority = attr->values[0].integer;
1259
1260                         if (strcmp(attr->name, "job-state") == 0 &&
1261                             attr->value_tag == IPP_TAG_ENUM)
1262                                 job_status = (ipp_jstate_t)(attr->values[0].integer);
1263
1264                         if (strcmp(attr->name, "time-at-creation") == 0 &&
1265                             attr->value_tag == IPP_TAG_INTEGER)
1266                                 job_time = attr->values[0].integer;
1267
1268                         if (strcmp(attr->name, "job-name") == 0 &&
1269                             attr->value_tag == IPP_TAG_NAME) {
1270                                 if (!pull_utf8_talloc(frame,
1271                                                 &job_name,
1272                                                 attr->values[0].string.text,
1273                                                 &size)) {
1274                                         goto out;
1275                                 }
1276                         }
1277
1278                         if (strcmp(attr->name, "job-originating-user-name") == 0 &&
1279                             attr->value_tag == IPP_TAG_NAME) {
1280                                 if (!pull_utf8_talloc(frame,
1281                                                 &user_name,
1282                                                 attr->values[0].string.text,
1283                                                 &size)) {
1284                                         goto out;
1285                                 }
1286                         }
1287
1288                         attr = attr->next;
1289                 }
1290
1291                /*
1292                 * See if we have everything needed...
1293                 */
1294
1295                 if (user_name == NULL || job_name == NULL || job_id == 0) {
1296                         if (attr == NULL)
1297                                 break;
1298                         else
1299                                 continue;
1300                 }
1301
1302                 temp->job      = job_id;
1303                 temp->size     = job_k_octets * 1024;
1304                 temp->status   = job_status == IPP_JOB_PENDING ? LPQ_QUEUED :
1305                                  job_status == IPP_JOB_STOPPED ? LPQ_PAUSED :
1306                                  job_status == IPP_JOB_HELD ? LPQ_PAUSED :
1307                                  LPQ_PRINTING;
1308                 temp->priority = job_priority;
1309                 temp->time     = job_time;
1310                 strlcpy(temp->fs_user, user_name, sizeof(temp->fs_user));
1311                 strlcpy(temp->fs_file, job_name, sizeof(temp->fs_file));
1312
1313                 qcount ++;
1314
1315                 if (attr == NULL)
1316                         break;
1317         }
1318
1319         ippDelete(response);
1320         response = NULL;
1321
1322        /*
1323         * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the
1324         * following attributes:
1325         *
1326         *    attributes-charset
1327         *    attributes-natural-language
1328         *    requested-attributes
1329         *    printer-uri
1330         */
1331
1332         request = ippNew();
1333
1334         request->request.op.operation_id = IPP_GET_PRINTER_ATTRIBUTES;
1335         request->request.op.request_id   = 1;
1336
1337         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1338                      "attributes-charset", NULL, "utf-8");
1339
1340         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1341                      "attributes-natural-language", NULL, language->language);
1342
1343         ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1344                       "requested-attributes",
1345                       (sizeof(pattrs) / sizeof(pattrs[0])),
1346                       NULL, pattrs);
1347
1348         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1349                      "printer-uri", NULL, uri);
1350
1351        /*
1352         * Do the request and get back a response...
1353         */
1354
1355         if ((response = cupsDoRequest(http, request, "/")) == NULL) {
1356                 DEBUG(0,("Unable to get printer status for %s - %s\n", printername,
1357                          ippErrorString(cupsLastError())));
1358                 goto out;
1359         }
1360
1361         if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1362                 DEBUG(0,("Unable to get printer status for %s - %s\n", printername,
1363                          ippErrorString(response->request.status.status_code)));
1364                 goto out;
1365         }
1366
1367        /*
1368         * Get the current printer status and convert it to the SAMBA values.
1369         */
1370
1371         if ((attr = ippFindAttribute(response, "printer-state", IPP_TAG_ENUM)) != NULL) {
1372                 if (attr->values[0].integer == IPP_PRINTER_STOPPED)
1373                         status->status = LPSTAT_STOPPED;
1374                 else
1375                         status->status = LPSTAT_OK;
1376         }
1377
1378         if ((attr = ippFindAttribute(response, "printer-state-message",
1379                                      IPP_TAG_TEXT)) != NULL) {
1380                 char *msg = NULL;
1381                 if (!pull_utf8_talloc(frame, &msg,
1382                                 attr->values[0].string.text,
1383                                 &size)) {
1384                         SAFE_FREE(queue);
1385                         qcount = 0;
1386                         goto out;
1387                 }
1388                 fstrcpy(status->message, msg);
1389         }
1390
1391  out:
1392
1393        /*
1394         * Return the job queue...
1395         */
1396
1397         *q = queue;
1398
1399         if (response)
1400                 ippDelete(response);
1401
1402         if (language)
1403                 cupsLangFree(language);
1404
1405         if (http)
1406                 httpClose(http);
1407
1408         TALLOC_FREE(frame);
1409         return qcount;
1410 }
1411
1412
1413 /*
1414  * 'cups_queue_pause()' - Pause a print queue.
1415  */
1416
1417 static int cups_queue_pause(int snum)
1418 {
1419         TALLOC_CTX *frame = talloc_stackframe();
1420         int             ret = 1;                /* Return value */
1421         http_t          *http = NULL;           /* HTTP connection to server */
1422         ipp_t           *request = NULL,        /* IPP Request */
1423                         *response = NULL;       /* IPP Response */
1424         cups_lang_t     *language = NULL;       /* Default language */
1425         char *printername = NULL;
1426         char *username = NULL;
1427         char            uri[HTTP_MAX_URI]; /* printer-uri attribute */
1428         size_t size;
1429
1430         DEBUG(5,("cups_queue_pause(%d)\n", snum));
1431
1432         /*
1433          * Make sure we don't ask for passwords...
1434          */
1435
1436         cupsSetPasswordCB(cups_passwd_cb);
1437
1438         /*
1439          * Try to connect to the server...
1440          */
1441
1442         if ((http = cups_connect(frame)) == NULL) {
1443                 goto out;
1444         }
1445
1446         /*
1447          * Build an IPP_PAUSE_PRINTER request, which requires the following
1448          * attributes:
1449          *
1450          *    attributes-charset
1451          *    attributes-natural-language
1452          *    printer-uri
1453          *    requesting-user-name
1454          */
1455
1456         request = ippNew();
1457
1458         request->request.op.operation_id = IPP_PAUSE_PRINTER;
1459         request->request.op.request_id   = 1;
1460
1461         language = cupsLangDefault();
1462
1463         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1464                      "attributes-charset", NULL, "utf-8");
1465
1466         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1467                      "attributes-natural-language", NULL, language->language);
1468
1469         if (!push_utf8_talloc(frame, &printername, lp_printername(snum),
1470                               &size)) {
1471                 goto out;
1472         }
1473         slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s",
1474                  printername);
1475
1476         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
1477
1478         if (!push_utf8_talloc(frame, &username, current_user_info.unix_name, &size)) {
1479                 goto out;
1480         }
1481         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
1482                      NULL, username);
1483
1484        /*
1485         * Do the request and get back a response...
1486         */
1487
1488         if ((response = cupsDoRequest(http, request, "/admin/")) != NULL) {
1489                 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1490                         DEBUG(0,("Unable to pause printer %s - %s\n",
1491                                  lp_printername(snum),
1492                                 ippErrorString(cupsLastError())));
1493                 } else {
1494                         ret = 0;
1495                 }
1496         } else {
1497                 DEBUG(0,("Unable to pause printer %s - %s\n",
1498                          lp_printername(snum),
1499                         ippErrorString(cupsLastError())));
1500         }
1501
1502  out:
1503         if (response)
1504                 ippDelete(response);
1505
1506         if (language)
1507                 cupsLangFree(language);
1508
1509         if (http)
1510                 httpClose(http);
1511
1512         TALLOC_FREE(frame);
1513         return ret;
1514 }
1515
1516
1517 /*
1518  * 'cups_queue_resume()' - Restart a print queue.
1519  */
1520
1521 static int cups_queue_resume(int snum)
1522 {
1523         TALLOC_CTX *frame = talloc_stackframe();
1524         int             ret = 1;                /* Return value */
1525         http_t          *http = NULL;           /* HTTP connection to server */
1526         ipp_t           *request = NULL,        /* IPP Request */
1527                         *response = NULL;       /* IPP Response */
1528         cups_lang_t     *language = NULL;       /* Default language */
1529         char *printername = NULL;
1530         char *username = NULL;
1531         char            uri[HTTP_MAX_URI]; /* printer-uri attribute */
1532         size_t size;
1533
1534         DEBUG(5,("cups_queue_resume(%d)\n", snum));
1535
1536        /*
1537         * Make sure we don't ask for passwords...
1538         */
1539
1540         cupsSetPasswordCB(cups_passwd_cb);
1541
1542        /*
1543         * Try to connect to the server...
1544         */
1545
1546         if ((http = cups_connect(frame)) == NULL) {
1547                 goto out;
1548         }
1549
1550        /*
1551         * Build an IPP_RESUME_PRINTER request, which requires the following
1552         * attributes:
1553         *
1554         *    attributes-charset
1555         *    attributes-natural-language
1556         *    printer-uri
1557         *    requesting-user-name
1558         */
1559
1560         request = ippNew();
1561
1562         request->request.op.operation_id = IPP_RESUME_PRINTER;
1563         request->request.op.request_id   = 1;
1564
1565         language = cupsLangDefault();
1566
1567         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1568                      "attributes-charset", NULL, "utf-8");
1569
1570         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1571                      "attributes-natural-language", NULL, language->language);
1572
1573         if (!push_utf8_talloc(frame, &printername, lp_printername(snum),
1574                               &size)) {
1575                 goto out;
1576         }
1577         slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s",
1578                  printername);
1579
1580         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
1581
1582         if (!push_utf8_talloc(frame, &username, current_user_info.unix_name, &size)) {
1583                 goto out;
1584         }
1585         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
1586                      NULL, username);
1587
1588        /*
1589         * Do the request and get back a response...
1590         */
1591
1592         if ((response = cupsDoRequest(http, request, "/admin/")) != NULL) {
1593                 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1594                         DEBUG(0,("Unable to resume printer %s - %s\n",
1595                                  lp_printername(snum),
1596                                 ippErrorString(cupsLastError())));
1597                 } else {
1598                         ret = 0;
1599                 }
1600         } else {
1601                 DEBUG(0,("Unable to resume printer %s - %s\n",
1602                          lp_printername(snum),
1603                         ippErrorString(cupsLastError())));
1604         }
1605
1606  out:
1607         if (response)
1608                 ippDelete(response);
1609
1610         if (language)
1611                 cupsLangFree(language);
1612
1613         if (http)
1614                 httpClose(http);
1615
1616         TALLOC_FREE(frame);
1617         return ret;
1618 }
1619
1620 /*******************************************************************
1621  * CUPS printing interface definitions...
1622  ******************************************************************/
1623
1624 struct printif  cups_printif =
1625 {
1626         PRINT_CUPS,
1627         cups_queue_get,
1628         cups_queue_pause,
1629         cups_queue_resume,
1630         cups_job_delete,
1631         cups_job_pause,
1632         cups_job_resume,
1633         cups_job_submit,
1634 };
1635
1636 bool cups_pull_comment_location(TALLOC_CTX *mem_ctx,
1637                                 const char *printername,
1638                                 char **comment,
1639                                 char **location)
1640 {
1641         TALLOC_CTX *frame = talloc_stackframe();
1642         http_t          *http = NULL;           /* HTTP connection to server */
1643         ipp_t           *request = NULL,        /* IPP Request */
1644                         *response = NULL;       /* IPP Response */
1645         ipp_attribute_t *attr;          /* Current attribute */
1646         cups_lang_t     *language = NULL;       /* Default language */
1647         char            uri[HTTP_MAX_URI];
1648         char *server = NULL;
1649         char *sharename = NULL;
1650         char *name = NULL;
1651         static const char *requested[] =/* Requested attributes */
1652                         {
1653                           "printer-name",
1654                           "printer-info",
1655                           "printer-location"
1656                         };
1657         bool ret = False;
1658         size_t size;
1659
1660         DEBUG(5, ("pulling %s location\n", printername));
1661
1662         /*
1663          * Make sure we don't ask for passwords...
1664          */
1665
1666         cupsSetPasswordCB(cups_passwd_cb);
1667
1668         /*
1669          * Try to connect to the server...
1670          */
1671
1672         if ((http = cups_connect(frame)) == NULL) {
1673                 goto out;
1674         }
1675
1676         request = ippNew();
1677
1678         request->request.op.operation_id = IPP_GET_PRINTER_ATTRIBUTES;
1679         request->request.op.request_id   = 1;
1680
1681         language = cupsLangDefault();
1682
1683         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1684                      "attributes-charset", NULL, "utf-8");
1685
1686         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1687                      "attributes-natural-language", NULL, language->language);
1688
1689         if (lp_cups_server() != NULL && strlen(lp_cups_server()) > 0) {
1690                 if (!push_utf8_talloc(frame, &server, lp_cups_server(), &size)) {
1691                         goto out;
1692                 }
1693         } else {
1694                 server = talloc_strdup(frame,cupsServer());
1695         }
1696         if (server) {
1697                 goto out;
1698         }
1699         if (!push_utf8_talloc(frame, &sharename, printername, &size)) {
1700                 goto out;
1701         }
1702         slprintf(uri, sizeof(uri) - 1, "ipp://%s/printers/%s",
1703                  server, sharename);
1704
1705         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1706                      "printer-uri", NULL, uri);
1707
1708         ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1709                       "requested-attributes",
1710                       (sizeof(requested) / sizeof(requested[0])),
1711                       NULL, requested);
1712
1713         /*
1714          * Do the request and get back a response...
1715          */
1716
1717         if ((response = cupsDoRequest(http, request, "/")) == NULL) {
1718                 DEBUG(0,("Unable to get printer attributes - %s\n",
1719                          ippErrorString(cupsLastError())));
1720                 goto out;
1721         }
1722
1723         for (attr = response->attrs; attr != NULL;) {
1724                 /*
1725                  * Skip leading attributes until we hit a printer...
1726                  */
1727
1728                 while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)
1729                         attr = attr->next;
1730
1731                 if (attr == NULL)
1732                         break;
1733
1734                 /*
1735                  * Pull the needed attributes from this printer...
1736                  */
1737
1738                 while ( attr && (attr->group_tag == IPP_TAG_PRINTER) ) {
1739                         if (strcmp(attr->name, "printer-name") == 0 &&
1740                             attr->value_tag == IPP_TAG_NAME) {
1741                                 if (!pull_utf8_talloc(frame,
1742                                                 &name,
1743                                                 attr->values[0].string.text,
1744                                                 &size)) {
1745                                         goto out;
1746                                 }
1747                         }
1748
1749                         /* Grab the comment if we don't have one */
1750                         if ( (strcmp(attr->name, "printer-info") == 0)
1751                              && (attr->value_tag == IPP_TAG_TEXT))
1752                         {
1753                                 if (!pull_utf8_talloc(mem_ctx,
1754                                                 comment,
1755                                                 attr->values[0].string.text,
1756                                                 &size)) {
1757                                         goto out;
1758                                 }
1759                                 DEBUG(5,("cups_pull_comment_location: Using cups comment: %s\n",
1760                                          *comment));
1761                         }
1762
1763                         /* Grab the location if we don't have one */
1764                         if ( (strcmp(attr->name, "printer-location") == 0)
1765                              && (attr->value_tag == IPP_TAG_TEXT))
1766                         {
1767                                 if (!pull_utf8_talloc(mem_ctx,
1768                                                 location,
1769                                                 attr->values[0].string.text,
1770                                                 &size)) {
1771                                         goto out;
1772                                 }
1773                                 DEBUG(5,("cups_pull_comment_location: Using cups location: %s\n",
1774                                          *location));
1775                         }
1776
1777                         attr = attr->next;
1778                 }
1779
1780                 /*
1781                  * We have everything needed...
1782                  */
1783
1784                 if (name != NULL)
1785                         break;
1786         }
1787
1788         ret = True;
1789
1790  out:
1791         if (response)
1792                 ippDelete(response);
1793
1794         if (request) {
1795                 ippDelete(request);
1796         }
1797
1798         if (language)
1799                 cupsLangFree(language);
1800
1801         if (http)
1802                 httpClose(http);
1803
1804         TALLOC_FREE(frame);
1805         return ret;
1806 }
1807
1808 #else
1809  /* this keeps fussy compilers happy */
1810  void print_cups_dummy(void);
1811  void print_cups_dummy(void) {}
1812 #endif /* HAVE_CUPS */