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