s3: Remove the pointless PRINTERNAME macro
[mat/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
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(server_messaging_context(),
438                                                server_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(server_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(server_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, lp_printername(snum),
966                               &size)) {
967                 goto out;
968         }
969         slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s",
970                  printername);
971
972         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
973                      "printer-uri", NULL, uri);
974
975         if (!push_utf8_talloc(frame, &user, pjob->user, &size)) {
976                 goto out;
977         }
978         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
979                      NULL, user);
980
981         clientname = client_name(get_client_fd());
982         if (strcmp(clientname, "UNKNOWN") == 0) {
983                 clientname = client_addr(get_client_fd(),addr,sizeof(addr));
984         }
985
986         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
987                      "job-originating-host-name", NULL,
988                       clientname);
989
990         /* Get the jobid from the filename. */
991         jobid = print_parse_jobid(pjob->filename);
992         if (jobid == (uint32_t)-1) {
993                 DEBUG(0,("cups_job_submit: failed to parse jobid from name %s\n",
994                                 pjob->filename ));
995                 jobid = 0;
996         }
997
998         if (!push_utf8_talloc(frame, &jobname, pjob->jobname, &size)) {
999                 goto out;
1000         }
1001         new_jobname = talloc_asprintf(frame,
1002                         "%s%.8u %s", PRINT_SPOOL_PREFIX,
1003                         (unsigned int)jobid,
1004                         jobname);
1005         if (new_jobname == NULL) {
1006                 goto out;
1007         }
1008
1009         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name", NULL,
1010                      new_jobname);
1011
1012         /*
1013          * add any options defined in smb.conf
1014          */
1015
1016         if (!push_utf8_talloc(frame, &cupsoptions, lp_cups_options(snum), &size)) {
1017                 goto out;
1018         }
1019         num_options = 0;
1020         options     = NULL;
1021         num_options = cupsParseOptions(cupsoptions, num_options, &options);
1022
1023         if ( num_options )
1024                 cupsEncodeOptions(request, num_options, options);
1025
1026        /*
1027         * Do the request and get back a response...
1028         */
1029
1030         slprintf(uri, sizeof(uri) - 1, "/printers/%s", printername);
1031
1032         if (!push_utf8_talloc(frame, &filename, pjob->filename, &size)) {
1033                 goto out;
1034         }
1035         if ((response = cupsDoFileRequest(http, request, uri, pjob->filename)) != NULL) {
1036                 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1037                         DEBUG(0,("Unable to print file to %s - %s\n",
1038                                  lp_printername(snum),
1039                                  ippErrorString(cupsLastError())));
1040                 } else {
1041                         ret = 0;
1042                         attr_job_id = ippFindAttribute(response, "job-id", IPP_TAG_INTEGER);
1043                         if(attr_job_id) {
1044                                 pjob->sysjob = attr_job_id->values[0].integer;
1045                                 DEBUG(5,("cups_job_submit: job-id %d\n", pjob->sysjob));
1046                         } else {
1047                                 DEBUG(0,("Missing job-id attribute in IPP response"));
1048                         }
1049                 }
1050         } else {
1051                 DEBUG(0,("Unable to print file to `%s' - %s\n",
1052                          lp_printername(snum),
1053                          ippErrorString(cupsLastError())));
1054         }
1055
1056         if ( ret == 0 )
1057                 unlink(pjob->filename);
1058         /* else print_job_end will do it for us */
1059
1060  out:
1061         if (response)
1062                 ippDelete(response);
1063
1064         if (language)
1065                 cupsLangFree(language);
1066
1067         if (http)
1068                 httpClose(http);
1069
1070         TALLOC_FREE(frame);
1071
1072         return ret;
1073 }
1074
1075 /*
1076  * 'cups_queue_get()' - Get all the jobs in the print queue.
1077  */
1078
1079 static int cups_queue_get(const char *sharename,
1080                enum printing_types printing_type,
1081                char *lpq_command,
1082                print_queue_struct **q,
1083                print_status_struct *status)
1084 {
1085         TALLOC_CTX *frame = talloc_stackframe();
1086         char *printername = NULL;
1087         http_t          *http = NULL;           /* HTTP connection to server */
1088         ipp_t           *request = NULL,        /* IPP Request */
1089                         *response = NULL;       /* IPP Response */
1090         ipp_attribute_t *attr = NULL;           /* Current attribute */
1091         cups_lang_t     *language = NULL;       /* Default language */
1092         char            uri[HTTP_MAX_URI]; /* printer-uri attribute */
1093         int             qcount = 0,             /* Number of active queue entries */
1094                         qalloc = 0;             /* Number of queue entries allocated */
1095         print_queue_struct *queue = NULL,       /* Queue entries */
1096                         *temp;          /* Temporary pointer for queue */
1097         char            *user_name = NULL,      /* job-originating-user-name attribute */
1098                         *job_name = NULL;       /* job-name attribute */
1099         int             job_id;         /* job-id attribute */
1100         int             job_k_octets;   /* job-k-octets attribute */
1101         time_t          job_time;       /* time-at-creation attribute */
1102         ipp_jstate_t    job_status;     /* job-status attribute */
1103         int             job_priority;   /* job-priority attribute */
1104         size_t size;
1105         static const char *jattrs[] =   /* Requested job attributes */
1106                         {
1107                           "job-id",
1108                           "job-k-octets",
1109                           "job-name",
1110                           "job-originating-user-name",
1111                           "job-priority",
1112                           "job-state",
1113                           "time-at-creation",
1114                         };
1115         static const char *pattrs[] =   /* Requested printer attributes */
1116                         {
1117                           "printer-state",
1118                           "printer-state-message"
1119                         };
1120
1121         *q = NULL;
1122
1123         /* HACK ALERT!!!  The problem with support the 'printer name'
1124            option is that we key the tdb off the sharename.  So we will
1125            overload the lpq_command string to pass in the printername
1126            (which is basically what we do for non-cups printers ... using
1127            the lpq_command to get the queue listing). */
1128
1129         if (!push_utf8_talloc(frame, &printername, lpq_command, &size)) {
1130                 goto out;
1131         }
1132         DEBUG(5,("cups_queue_get(%s, %p, %p)\n", lpq_command, q, status));
1133
1134        /*
1135         * Make sure we don't ask for passwords...
1136         */
1137
1138         cupsSetPasswordCB(cups_passwd_cb);
1139
1140        /*
1141         * Try to connect to the server...
1142         */
1143
1144         if ((http = cups_connect(frame)) == NULL) {
1145                 goto out;
1146         }
1147
1148        /*
1149         * Generate the printer URI...
1150         */
1151
1152         slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s", printername);
1153
1154        /*
1155         * Build an IPP_GET_JOBS request, which requires the following
1156         * attributes:
1157         *
1158         *    attributes-charset
1159         *    attributes-natural-language
1160         *    requested-attributes
1161         *    printer-uri
1162         */
1163
1164         request = ippNew();
1165
1166         request->request.op.operation_id = IPP_GET_JOBS;
1167         request->request.op.request_id   = 1;
1168
1169         language = cupsLangDefault();
1170
1171         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1172                      "attributes-charset", NULL, "utf-8");
1173
1174         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1175                      "attributes-natural-language", NULL, language->language);
1176
1177         ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1178                       "requested-attributes",
1179                       (sizeof(jattrs) / sizeof(jattrs[0])),
1180                       NULL, jattrs);
1181
1182         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1183                      "printer-uri", NULL, uri);
1184
1185        /*
1186         * Do the request and get back a response...
1187         */
1188
1189         if ((response = cupsDoRequest(http, request, "/")) == NULL) {
1190                 DEBUG(0,("Unable to get jobs for %s - %s\n", uri,
1191                          ippErrorString(cupsLastError())));
1192                 goto out;
1193         }
1194
1195         if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1196                 DEBUG(0,("Unable to get jobs for %s - %s\n", uri,
1197                          ippErrorString(response->request.status.status_code)));
1198                 goto out;
1199         }
1200
1201        /*
1202         * Process the jobs...
1203         */
1204
1205         qcount = 0;
1206         qalloc = 0;
1207         queue  = NULL;
1208
1209         for (attr = response->attrs; attr != NULL; attr = attr->next) {
1210                /*
1211                 * Skip leading attributes until we hit a job...
1212                 */
1213
1214                 while (attr != NULL && attr->group_tag != IPP_TAG_JOB)
1215                         attr = attr->next;
1216
1217                 if (attr == NULL)
1218                         break;
1219
1220                /*
1221                 * Allocate memory as needed...
1222                 */
1223                 if (qcount >= qalloc) {
1224                         qalloc += 16;
1225
1226                         queue = SMB_REALLOC_ARRAY(queue, print_queue_struct, qalloc);
1227
1228                         if (queue == NULL) {
1229                                 DEBUG(0,("cups_queue_get: Not enough memory!"));
1230                                 qcount = 0;
1231                                 goto out;
1232                         }
1233                 }
1234
1235                 temp = queue + qcount;
1236                 memset(temp, 0, sizeof(print_queue_struct));
1237
1238                /*
1239                 * Pull the needed attributes from this job...
1240                 */
1241
1242                 job_id       = 0;
1243                 job_priority = 50;
1244                 job_status   = IPP_JOB_PENDING;
1245                 job_time     = 0;
1246                 job_k_octets = 0;
1247                 user_name    = NULL;
1248                 job_name     = NULL;
1249
1250                 while (attr != NULL && attr->group_tag == IPP_TAG_JOB) {
1251                         if (attr->name == NULL) {
1252                                 attr = attr->next;
1253                                 break;
1254                         }
1255
1256                         if (strcmp(attr->name, "job-id") == 0 &&
1257                             attr->value_tag == IPP_TAG_INTEGER)
1258                                 job_id = attr->values[0].integer;
1259
1260                         if (strcmp(attr->name, "job-k-octets") == 0 &&
1261                             attr->value_tag == IPP_TAG_INTEGER)
1262                                 job_k_octets = attr->values[0].integer;
1263
1264                         if (strcmp(attr->name, "job-priority") == 0 &&
1265                             attr->value_tag == IPP_TAG_INTEGER)
1266                                 job_priority = attr->values[0].integer;
1267
1268                         if (strcmp(attr->name, "job-state") == 0 &&
1269                             attr->value_tag == IPP_TAG_ENUM)
1270                                 job_status = (ipp_jstate_t)(attr->values[0].integer);
1271
1272                         if (strcmp(attr->name, "time-at-creation") == 0 &&
1273                             attr->value_tag == IPP_TAG_INTEGER)
1274                                 job_time = attr->values[0].integer;
1275
1276                         if (strcmp(attr->name, "job-name") == 0 &&
1277                             attr->value_tag == IPP_TAG_NAME) {
1278                                 if (!pull_utf8_talloc(frame,
1279                                                 &job_name,
1280                                                 attr->values[0].string.text,
1281                                                 &size)) {
1282                                         goto out;
1283                                 }
1284                         }
1285
1286                         if (strcmp(attr->name, "job-originating-user-name") == 0 &&
1287                             attr->value_tag == IPP_TAG_NAME) {
1288                                 if (!pull_utf8_talloc(frame,
1289                                                 &user_name,
1290                                                 attr->values[0].string.text,
1291                                                 &size)) {
1292                                         goto out;
1293                                 }
1294                         }
1295
1296                         attr = attr->next;
1297                 }
1298
1299                /*
1300                 * See if we have everything needed...
1301                 */
1302
1303                 if (user_name == NULL || job_name == NULL || job_id == 0) {
1304                         if (attr == NULL)
1305                                 break;
1306                         else
1307                                 continue;
1308                 }
1309
1310                 temp->job      = job_id;
1311                 temp->size     = job_k_octets * 1024;
1312                 temp->status   = job_status == IPP_JOB_PENDING ? LPQ_QUEUED :
1313                                  job_status == IPP_JOB_STOPPED ? LPQ_PAUSED :
1314                                  job_status == IPP_JOB_HELD ? LPQ_PAUSED :
1315                                  LPQ_PRINTING;
1316                 temp->priority = job_priority;
1317                 temp->time     = job_time;
1318                 strlcpy(temp->fs_user, user_name, sizeof(temp->fs_user));
1319                 strlcpy(temp->fs_file, job_name, sizeof(temp->fs_file));
1320
1321                 qcount ++;
1322
1323                 if (attr == NULL)
1324                         break;
1325         }
1326
1327         ippDelete(response);
1328         response = NULL;
1329
1330        /*
1331         * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the
1332         * following attributes:
1333         *
1334         *    attributes-charset
1335         *    attributes-natural-language
1336         *    requested-attributes
1337         *    printer-uri
1338         */
1339
1340         request = ippNew();
1341
1342         request->request.op.operation_id = IPP_GET_PRINTER_ATTRIBUTES;
1343         request->request.op.request_id   = 1;
1344
1345         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1346                      "attributes-charset", NULL, "utf-8");
1347
1348         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1349                      "attributes-natural-language", NULL, language->language);
1350
1351         ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1352                       "requested-attributes",
1353                       (sizeof(pattrs) / sizeof(pattrs[0])),
1354                       NULL, pattrs);
1355
1356         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1357                      "printer-uri", NULL, uri);
1358
1359        /*
1360         * Do the request and get back a response...
1361         */
1362
1363         if ((response = cupsDoRequest(http, request, "/")) == NULL) {
1364                 DEBUG(0,("Unable to get printer status for %s - %s\n", printername,
1365                          ippErrorString(cupsLastError())));
1366                 goto out;
1367         }
1368
1369         if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1370                 DEBUG(0,("Unable to get printer status for %s - %s\n", printername,
1371                          ippErrorString(response->request.status.status_code)));
1372                 goto out;
1373         }
1374
1375        /*
1376         * Get the current printer status and convert it to the SAMBA values.
1377         */
1378
1379         if ((attr = ippFindAttribute(response, "printer-state", IPP_TAG_ENUM)) != NULL) {
1380                 if (attr->values[0].integer == IPP_PRINTER_STOPPED)
1381                         status->status = LPSTAT_STOPPED;
1382                 else
1383                         status->status = LPSTAT_OK;
1384         }
1385
1386         if ((attr = ippFindAttribute(response, "printer-state-message",
1387                                      IPP_TAG_TEXT)) != NULL) {
1388                 char *msg = NULL;
1389                 if (!pull_utf8_talloc(frame, &msg,
1390                                 attr->values[0].string.text,
1391                                 &size)) {
1392                         SAFE_FREE(queue);
1393                         qcount = 0;
1394                         goto out;
1395                 }
1396                 fstrcpy(status->message, msg);
1397         }
1398
1399  out:
1400
1401        /*
1402         * Return the job queue...
1403         */
1404
1405         *q = queue;
1406
1407         if (response)
1408                 ippDelete(response);
1409
1410         if (language)
1411                 cupsLangFree(language);
1412
1413         if (http)
1414                 httpClose(http);
1415
1416         TALLOC_FREE(frame);
1417         return qcount;
1418 }
1419
1420
1421 /*
1422  * 'cups_queue_pause()' - Pause a print queue.
1423  */
1424
1425 static int cups_queue_pause(int snum)
1426 {
1427         TALLOC_CTX *frame = talloc_stackframe();
1428         int             ret = 1;                /* Return value */
1429         http_t          *http = NULL;           /* HTTP connection to server */
1430         ipp_t           *request = NULL,        /* IPP Request */
1431                         *response = NULL;       /* IPP Response */
1432         cups_lang_t     *language = NULL;       /* Default language */
1433         char *printername = NULL;
1434         char *username = NULL;
1435         char            uri[HTTP_MAX_URI]; /* printer-uri attribute */
1436         size_t size;
1437
1438         DEBUG(5,("cups_queue_pause(%d)\n", snum));
1439
1440         /*
1441          * Make sure we don't ask for passwords...
1442          */
1443
1444         cupsSetPasswordCB(cups_passwd_cb);
1445
1446         /*
1447          * Try to connect to the server...
1448          */
1449
1450         if ((http = cups_connect(frame)) == NULL) {
1451                 goto out;
1452         }
1453
1454         /*
1455          * Build an IPP_PAUSE_PRINTER request, which requires the following
1456          * attributes:
1457          *
1458          *    attributes-charset
1459          *    attributes-natural-language
1460          *    printer-uri
1461          *    requesting-user-name
1462          */
1463
1464         request = ippNew();
1465
1466         request->request.op.operation_id = IPP_PAUSE_PRINTER;
1467         request->request.op.request_id   = 1;
1468
1469         language = cupsLangDefault();
1470
1471         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1472                      "attributes-charset", NULL, "utf-8");
1473
1474         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1475                      "attributes-natural-language", NULL, language->language);
1476
1477         if (!push_utf8_talloc(frame, &printername, lp_printername(snum),
1478                               &size)) {
1479                 goto out;
1480         }
1481         slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s",
1482                  printername);
1483
1484         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
1485
1486         if (!push_utf8_talloc(frame, &username, current_user_info.unix_name, &size)) {
1487                 goto out;
1488         }
1489         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
1490                      NULL, username);
1491
1492        /*
1493         * Do the request and get back a response...
1494         */
1495
1496         if ((response = cupsDoRequest(http, request, "/admin/")) != NULL) {
1497                 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1498                         DEBUG(0,("Unable to pause printer %s - %s\n",
1499                                  lp_printername(snum),
1500                                 ippErrorString(cupsLastError())));
1501                 } else {
1502                         ret = 0;
1503                 }
1504         } else {
1505                 DEBUG(0,("Unable to pause printer %s - %s\n",
1506                          lp_printername(snum),
1507                         ippErrorString(cupsLastError())));
1508         }
1509
1510  out:
1511         if (response)
1512                 ippDelete(response);
1513
1514         if (language)
1515                 cupsLangFree(language);
1516
1517         if (http)
1518                 httpClose(http);
1519
1520         TALLOC_FREE(frame);
1521         return ret;
1522 }
1523
1524
1525 /*
1526  * 'cups_queue_resume()' - Restart a print queue.
1527  */
1528
1529 static int cups_queue_resume(int snum)
1530 {
1531         TALLOC_CTX *frame = talloc_stackframe();
1532         int             ret = 1;                /* Return value */
1533         http_t          *http = NULL;           /* HTTP connection to server */
1534         ipp_t           *request = NULL,        /* IPP Request */
1535                         *response = NULL;       /* IPP Response */
1536         cups_lang_t     *language = NULL;       /* Default language */
1537         char *printername = NULL;
1538         char *username = NULL;
1539         char            uri[HTTP_MAX_URI]; /* printer-uri attribute */
1540         size_t size;
1541
1542         DEBUG(5,("cups_queue_resume(%d)\n", snum));
1543
1544        /*
1545         * Make sure we don't ask for passwords...
1546         */
1547
1548         cupsSetPasswordCB(cups_passwd_cb);
1549
1550        /*
1551         * Try to connect to the server...
1552         */
1553
1554         if ((http = cups_connect(frame)) == NULL) {
1555                 goto out;
1556         }
1557
1558        /*
1559         * Build an IPP_RESUME_PRINTER request, which requires the following
1560         * attributes:
1561         *
1562         *    attributes-charset
1563         *    attributes-natural-language
1564         *    printer-uri
1565         *    requesting-user-name
1566         */
1567
1568         request = ippNew();
1569
1570         request->request.op.operation_id = IPP_RESUME_PRINTER;
1571         request->request.op.request_id   = 1;
1572
1573         language = cupsLangDefault();
1574
1575         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1576                      "attributes-charset", NULL, "utf-8");
1577
1578         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1579                      "attributes-natural-language", NULL, language->language);
1580
1581         if (!push_utf8_talloc(frame, &printername, lp_printername(snum),
1582                               &size)) {
1583                 goto out;
1584         }
1585         slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s",
1586                  printername);
1587
1588         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
1589
1590         if (!push_utf8_talloc(frame, &username, current_user_info.unix_name, &size)) {
1591                 goto out;
1592         }
1593         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
1594                      NULL, username);
1595
1596        /*
1597         * Do the request and get back a response...
1598         */
1599
1600         if ((response = cupsDoRequest(http, request, "/admin/")) != NULL) {
1601                 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1602                         DEBUG(0,("Unable to resume printer %s - %s\n",
1603                                  lp_printername(snum),
1604                                 ippErrorString(cupsLastError())));
1605                 } else {
1606                         ret = 0;
1607                 }
1608         } else {
1609                 DEBUG(0,("Unable to resume printer %s - %s\n",
1610                          lp_printername(snum),
1611                         ippErrorString(cupsLastError())));
1612         }
1613
1614  out:
1615         if (response)
1616                 ippDelete(response);
1617
1618         if (language)
1619                 cupsLangFree(language);
1620
1621         if (http)
1622                 httpClose(http);
1623
1624         TALLOC_FREE(frame);
1625         return ret;
1626 }
1627
1628 /*******************************************************************
1629  * CUPS printing interface definitions...
1630  ******************************************************************/
1631
1632 struct printif  cups_printif =
1633 {
1634         PRINT_CUPS,
1635         cups_queue_get,
1636         cups_queue_pause,
1637         cups_queue_resume,
1638         cups_job_delete,
1639         cups_job_pause,
1640         cups_job_resume,
1641         cups_job_submit,
1642 };
1643
1644 bool cups_pull_comment_location(TALLOC_CTX *mem_ctx,
1645                                 const char *printername,
1646                                 char **comment,
1647                                 char **location)
1648 {
1649         TALLOC_CTX *frame = talloc_stackframe();
1650         http_t          *http = NULL;           /* HTTP connection to server */
1651         ipp_t           *request = NULL,        /* IPP Request */
1652                         *response = NULL;       /* IPP Response */
1653         ipp_attribute_t *attr;          /* Current attribute */
1654         cups_lang_t     *language = NULL;       /* Default language */
1655         char            uri[HTTP_MAX_URI];
1656         char *server = NULL;
1657         char *sharename = NULL;
1658         char *name = NULL;
1659         static const char *requested[] =/* Requested attributes */
1660                         {
1661                           "printer-name",
1662                           "printer-info",
1663                           "printer-location"
1664                         };
1665         bool ret = False;
1666         size_t size;
1667
1668         DEBUG(5, ("pulling %s location\n", printername));
1669
1670         /*
1671          * Make sure we don't ask for passwords...
1672          */
1673
1674         cupsSetPasswordCB(cups_passwd_cb);
1675
1676         /*
1677          * Try to connect to the server...
1678          */
1679
1680         if ((http = cups_connect(frame)) == NULL) {
1681                 goto out;
1682         }
1683
1684         request = ippNew();
1685
1686         request->request.op.operation_id = IPP_GET_PRINTER_ATTRIBUTES;
1687         request->request.op.request_id   = 1;
1688
1689         language = cupsLangDefault();
1690
1691         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1692                      "attributes-charset", NULL, "utf-8");
1693
1694         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1695                      "attributes-natural-language", NULL, language->language);
1696
1697         if (lp_cups_server() != NULL && strlen(lp_cups_server()) > 0) {
1698                 if (!push_utf8_talloc(frame, &server, lp_cups_server(), &size)) {
1699                         goto out;
1700                 }
1701         } else {
1702                 server = talloc_strdup(frame,cupsServer());
1703         }
1704         if (server) {
1705                 goto out;
1706         }
1707         if (!push_utf8_talloc(frame, &sharename, printername, &size)) {
1708                 goto out;
1709         }
1710         slprintf(uri, sizeof(uri) - 1, "ipp://%s/printers/%s",
1711                  server, sharename);
1712
1713         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1714                      "printer-uri", NULL, uri);
1715
1716         ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1717                       "requested-attributes",
1718                       (sizeof(requested) / sizeof(requested[0])),
1719                       NULL, requested);
1720
1721         /*
1722          * Do the request and get back a response...
1723          */
1724
1725         if ((response = cupsDoRequest(http, request, "/")) == NULL) {
1726                 DEBUG(0,("Unable to get printer attributes - %s\n",
1727                          ippErrorString(cupsLastError())));
1728                 goto out;
1729         }
1730
1731         for (attr = response->attrs; attr != NULL;) {
1732                 /*
1733                  * Skip leading attributes until we hit a printer...
1734                  */
1735
1736                 while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)
1737                         attr = attr->next;
1738
1739                 if (attr == NULL)
1740                         break;
1741
1742                 /*
1743                  * Pull the needed attributes from this printer...
1744                  */
1745
1746                 while ( attr && (attr->group_tag == IPP_TAG_PRINTER) ) {
1747                         if (strcmp(attr->name, "printer-name") == 0 &&
1748                             attr->value_tag == IPP_TAG_NAME) {
1749                                 if (!pull_utf8_talloc(frame,
1750                                                 &name,
1751                                                 attr->values[0].string.text,
1752                                                 &size)) {
1753                                         goto out;
1754                                 }
1755                         }
1756
1757                         /* Grab the comment if we don't have one */
1758                         if ( (strcmp(attr->name, "printer-info") == 0)
1759                              && (attr->value_tag == IPP_TAG_TEXT))
1760                         {
1761                                 if (!pull_utf8_talloc(mem_ctx,
1762                                                 comment,
1763                                                 attr->values[0].string.text,
1764                                                 &size)) {
1765                                         goto out;
1766                                 }
1767                                 DEBUG(5,("cups_pull_comment_location: Using cups comment: %s\n",
1768                                          *comment));
1769                         }
1770
1771                         /* Grab the location if we don't have one */
1772                         if ( (strcmp(attr->name, "printer-location") == 0)
1773                              && (attr->value_tag == IPP_TAG_TEXT))
1774                         {
1775                                 if (!pull_utf8_talloc(mem_ctx,
1776                                                 location,
1777                                                 attr->values[0].string.text,
1778                                                 &size)) {
1779                                         goto out;
1780                                 }
1781                                 DEBUG(5,("cups_pull_comment_location: Using cups location: %s\n",
1782                                          *location));
1783                         }
1784
1785                         attr = attr->next;
1786                 }
1787
1788                 /*
1789                  * We have everything needed...
1790                  */
1791
1792                 if (name != NULL)
1793                         break;
1794         }
1795
1796         ret = True;
1797
1798  out:
1799         if (response)
1800                 ippDelete(response);
1801
1802         if (request) {
1803                 ippDelete(request);
1804         }
1805
1806         if (language)
1807                 cupsLangFree(language);
1808
1809         if (http)
1810                 httpClose(http);
1811
1812         TALLOC_FREE(frame);
1813         return ret;
1814 }
1815
1816 #else
1817  /* this keeps fussy compilers happy */
1818  void print_cups_dummy(void);
1819  void print_cups_dummy(void) {}
1820 #endif /* HAVE_CUPS */