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