s3:idmap_ad: add support for ADS_AUTH_SASL_{STARTTLS,LDAPS}
[Samba.git] / source3 / printing / print_cups.c
blobd8ba9ccd011ca82413f442f9b3118ea7442ffe79
1 /*
2 * Support code for the Common UNIX Printing System ("CUPS")
4 * Copyright 1999-2003 by Michael R Sweet.
5 * Copyright 2008 Jeremy Allison.
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.
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.
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/>.
22 * JRA. Converted to utf8 pull/push.
25 #include "includes.h"
26 #include "printing.h"
27 #include "printing/pcap.h"
28 #include "librpc/gen_ndr/ndr_printcap.h"
29 #include "lib/util/sys_rw.h"
30 #include "lib/util/string_wrappers.h"
32 #ifdef HAVE_CUPS
33 #include <cups/cups.h>
34 #include <cups/language.h>
35 #include <cups/http.h>
37 /* CUPS prior to version 1.7 doesn't have HTTP_URI_STATUS_OK */
38 #if (CUPS_VERSION_MAJOR == 1) && (CUPS_VERSION_MINOR < 7)
39 #define HTTP_URI_STATUS_OK HTTP_URI_OK
40 #endif
42 #if (CUPS_VERSION_MAJOR > 1) || (CUPS_VERSION_MINOR > 5)
43 #define HAVE_CUPS_1_6 1
44 #endif
46 #ifndef HAVE_CUPS_1_6
47 #define ippGetGroupTag(attr) attr->group_tag
48 #define ippGetName(attr) attr->name
49 #define ippGetValueTag(attr) attr->value_tag
50 #define ippGetStatusCode(ipp) ipp->request.status.status_code
51 #define ippGetInteger(attr, element) attr->values[element].integer
52 #define ippGetString(attr, element, language) attr->values[element].string.text
54 static ipp_attribute_t *
55 ippFirstAttribute(ipp_t *ipp)
57 if (!ipp)
58 return (NULL);
59 return (ipp->current = ipp->attrs);
62 static ipp_attribute_t *
63 ippNextAttribute(ipp_t *ipp)
65 if (!ipp || !ipp->current)
66 return (NULL);
67 return (ipp->current = ipp->current->next);
70 static int ippSetOperation(ipp_t *ipp, ipp_op_t op)
72 ipp->request.op.operation_id = op;
73 return (1);
76 static int ippSetRequestId(ipp_t *ipp, int request_id)
78 ipp->request.any.request_id = request_id;
79 return (1);
81 #endif
83 static SIG_ATOMIC_T gotalarm;
85 /***************************************************************
86 Signal function to tell us we timed out.
87 ****************************************************************/
89 static void gotalarm_sig(int signum)
91 gotalarm = 1;
94 extern userdom_struct current_user_info;
97 * 'cups_passwd_cb()' - The CUPS password callback...
100 static const char * /* O - Password or NULL */
101 cups_passwd_cb(const char *prompt) /* I - Prompt */
104 * Always return NULL to indicate that no password is available...
107 return (NULL);
110 static http_t *cups_connect(TALLOC_CTX *frame)
112 const struct loadparm_substitution *lp_sub =
113 loadparm_s3_global_substitution();
114 http_t *http = NULL;
115 char *server = NULL, *p = NULL;
116 int port;
117 int timeout = lp_cups_connection_timeout();
118 size_t size;
120 if (lp_cups_server(talloc_tos(), lp_sub) != NULL && strlen(lp_cups_server(talloc_tos(), lp_sub)) > 0) {
121 if (!push_utf8_talloc(frame, &server, lp_cups_server(talloc_tos(), lp_sub), &size)) {
122 return NULL;
124 } else {
125 server = talloc_strdup(frame,cupsServer());
127 if (!server) {
128 return NULL;
131 p = strchr(server, ':');
132 if (p) {
133 port = atoi(p+1);
134 *p = '\0';
135 } else {
136 port = ippPort();
139 DEBUG(10, ("connecting to cups server %s:%d\n",
140 server, port));
142 gotalarm = 0;
144 if (timeout) {
145 CatchSignal(SIGALRM, gotalarm_sig);
146 alarm(timeout);
149 #if defined(HAVE_HTTPCONNECT2)
150 http = httpConnect2(server,
151 port,
152 NULL,
153 AF_UNSPEC,
154 lp_cups_encrypt() ?
155 HTTP_ENCRYPTION_ALWAYS :
156 HTTP_ENCRYPTION_IF_REQUESTED,
157 1, /* blocking */
158 30 * 1000, /* timeout */
159 NULL);
160 #elif defined(HAVE_HTTPCONNECTENCRYPT)
161 http = httpConnectEncrypt(server, port, lp_cups_encrypt());
162 #else
163 http = httpConnect(server, port);
164 #endif
167 CatchSignal(SIGALRM, SIG_IGN);
168 alarm(0);
170 if (http == NULL) {
171 DEBUG(3,("Unable to connect to CUPS server %s:%d - %s\n",
172 server, port, strerror(errno)));
175 return http;
178 static bool send_pcap_blob(DATA_BLOB *pcap_blob, int fd)
180 size_t ret;
182 ret = sys_write(fd, &pcap_blob->length, sizeof(pcap_blob->length));
183 if (ret != sizeof(pcap_blob->length)) {
184 return false;
187 ret = sys_write(fd, pcap_blob->data, pcap_blob->length);
188 if (ret != pcap_blob->length) {
189 return false;
192 DEBUG(10, ("successfully sent blob of len %d\n", (int)ret));
193 return true;
196 static bool recv_pcap_blob(TALLOC_CTX *mem_ctx, int fd, DATA_BLOB *pcap_blob)
198 size_t blob_len;
199 size_t ret;
201 ret = sys_read(fd, &blob_len, sizeof(blob_len));
202 if (ret != sizeof(blob_len)) {
203 return false;
206 *pcap_blob = data_blob_talloc_named(mem_ctx, NULL, blob_len,
207 "cups pcap");
208 if (pcap_blob->length != blob_len) {
209 return false;
211 ret = sys_read(fd, pcap_blob->data, blob_len);
212 if (ret != blob_len) {
213 talloc_free(pcap_blob->data);
214 return false;
217 DEBUG(10, ("successfully recvd blob of len %d\n", (int)ret));
218 return true;
221 static bool process_cups_printers_response(TALLOC_CTX *mem_ctx,
222 ipp_t *response,
223 struct pcap_data *pcap_data)
225 ipp_attribute_t *attr;
226 char *name;
227 char *info;
228 char *location = NULL;
229 struct pcap_printer *printer;
230 bool ret_ok = false;
232 for (attr = ippFirstAttribute(response); attr != NULL;) {
234 * Skip leading attributes until we hit a printer...
237 while (attr != NULL && ippGetGroupTag(attr) != IPP_TAG_PRINTER)
238 attr = ippNextAttribute(response);
240 if (attr == NULL)
241 break;
244 * Pull the needed attributes from this printer...
247 name = NULL;
248 info = NULL;
250 while (attr != NULL && ippGetGroupTag(attr) == IPP_TAG_PRINTER) {
251 size_t size;
252 if (strcmp(ippGetName(attr), "printer-name") == 0 &&
253 ippGetValueTag(attr) == IPP_TAG_NAME) {
254 if (!pull_utf8_talloc(mem_ctx,
255 &name,
256 ippGetString(attr, 0, NULL),
257 &size)) {
258 goto err_out;
262 if (strcmp(ippGetName(attr), "printer-info") == 0 &&
263 ippGetValueTag(attr) == IPP_TAG_TEXT) {
264 if (!pull_utf8_talloc(mem_ctx,
265 &info,
266 ippGetString(attr, 0, NULL),
267 &size)) {
268 goto err_out;
272 if (strcmp(ippGetName(attr), "printer-location") == 0 &&
273 ippGetValueTag(attr) == IPP_TAG_TEXT) {
274 if (!pull_utf8_talloc(mem_ctx,
275 &location,
276 ippGetString(attr, 0, NULL),
277 &size)) {
278 goto err_out;
282 attr = ippNextAttribute(response);
286 * See if we have everything needed...
289 if (name == NULL)
290 break;
292 if (pcap_data->count == 0) {
293 printer = talloc_array(mem_ctx, struct pcap_printer, 1);
294 } else {
295 printer = talloc_realloc(mem_ctx, pcap_data->printers,
296 struct pcap_printer,
297 pcap_data->count + 1);
299 if (printer == NULL) {
300 goto err_out;
302 pcap_data->printers = printer;
303 pcap_data->printers[pcap_data->count].name = name;
304 pcap_data->printers[pcap_data->count].info = info;
305 pcap_data->printers[pcap_data->count].location = location;
306 pcap_data->count++;
309 ret_ok = true;
310 err_out:
311 return ret_ok;
315 * request printer list from cups, send result back to up parent via fd.
316 * returns true if the (possibly failed) result was successfully sent to parent.
318 static bool cups_cache_reload_async(int fd)
320 TALLOC_CTX *frame = talloc_stackframe();
321 struct pcap_data pcap_data;
322 http_t *http = NULL; /* HTTP connection to server */
323 ipp_t *request = NULL, /* IPP Request */
324 *response = NULL; /* IPP Response */
325 cups_lang_t *language = NULL; /* Default language */
326 static const char *requested[] =/* Requested attributes */
328 "printer-name",
329 "printer-info",
330 "printer-location"
332 bool ret = False;
333 enum ndr_err_code ndr_ret;
334 DATA_BLOB pcap_blob;
336 ZERO_STRUCT(pcap_data);
337 pcap_data.status = NT_STATUS_UNSUCCESSFUL;
339 DEBUG(5, ("reloading cups printcap cache\n"));
342 * Make sure we don't ask for passwords...
345 cupsSetPasswordCB(cups_passwd_cb);
347 if ((http = cups_connect(frame)) == NULL) {
348 goto out;
352 * Build a CUPS_GET_PRINTERS request, which requires the following
353 * attributes:
355 * attributes-charset
356 * attributes-natural-language
357 * requested-attributes
360 request = ippNew();
362 ippSetOperation(request, CUPS_GET_PRINTERS);
363 ippSetRequestId(request, 1);
365 language = cupsLangDefault();
367 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
368 "attributes-charset", NULL, "utf-8");
370 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
371 "attributes-natural-language", NULL, language->language);
373 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
374 "requested-attributes",
375 (sizeof(requested) / sizeof(requested[0])),
376 NULL, requested);
378 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
379 DEBUG(0,("Unable to get printer list - %s\n",
380 ippErrorString(cupsLastError())));
381 goto out;
384 ret = process_cups_printers_response(frame, response, &pcap_data);
385 if (!ret) {
386 DEBUG(0,("failed to process cups response\n"));
387 goto out;
390 ippDelete(response);
391 response = NULL;
394 * Build a CUPS_GET_CLASSES request, which requires the following
395 * attributes:
397 * attributes-charset
398 * attributes-natural-language
399 * requested-attributes
402 request = ippNew();
404 ippSetOperation(request, CUPS_GET_CLASSES);
405 ippSetRequestId(request, 1);
407 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
408 "attributes-charset", NULL, "utf-8");
410 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
411 "attributes-natural-language", NULL, language->language);
413 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
414 "requested-attributes",
415 (sizeof(requested) / sizeof(requested[0])),
416 NULL, requested);
418 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
419 DEBUG(0,("Unable to get printer list - %s\n",
420 ippErrorString(cupsLastError())));
421 goto out;
424 ret = process_cups_printers_response(frame, response, &pcap_data);
425 if (!ret) {
426 DEBUG(0,("failed to process cups response\n"));
427 goto out;
430 pcap_data.status = NT_STATUS_OK;
431 out:
432 if (response)
433 ippDelete(response);
435 if (language)
436 cupsLangFree(language);
438 if (http)
439 httpClose(http);
441 ret = false;
442 ndr_ret = ndr_push_struct_blob(&pcap_blob, frame, &pcap_data,
443 (ndr_push_flags_fn_t)ndr_push_pcap_data);
444 if (ndr_ret == NDR_ERR_SUCCESS) {
445 ret = send_pcap_blob(&pcap_blob, fd);
448 TALLOC_FREE(frame);
449 return ret;
452 static struct tevent_fd *cache_fd_event;
454 static bool cups_pcap_load_async(struct tevent_context *ev,
455 struct messaging_context *msg_ctx,
456 int *pfd)
458 int fds[2];
459 pid_t pid;
460 NTSTATUS status;
462 *pfd = -1;
464 if (cache_fd_event) {
465 DEBUG(3,("cups_pcap_load_async: already waiting for "
466 "a refresh event\n" ));
467 return false;
470 DEBUG(5,("cups_pcap_load_async: asynchronously loading cups printers\n"));
472 if (pipe(fds) == -1) {
473 return false;
476 pid = fork();
477 if (pid == (pid_t)-1) {
478 DEBUG(10,("cups_pcap_load_async: fork failed %s\n",
479 strerror(errno) ));
480 close(fds[0]);
481 close(fds[1]);
482 return false;
485 if (pid) {
486 DEBUG(10,("cups_pcap_load_async: child pid = %u\n",
487 (unsigned int)pid ));
488 /* Parent. */
489 close(fds[1]);
490 *pfd = fds[0];
491 return true;
494 /* Child. */
496 close_all_print_db();
498 status = reinit_after_fork(msg_ctx, ev, true);
499 if (!NT_STATUS_IS_OK(status)) {
500 DEBUG(0,("cups_pcap_load_async: reinit_after_fork() failed\n"));
501 smb_panic("cups_pcap_load_async: reinit_after_fork() failed");
504 close(fds[0]);
505 cups_cache_reload_async(fds[1]);
506 close(fds[1]);
507 TALLOC_FREE(msg_ctx);
508 _exit(0);
511 struct cups_async_cb_args {
512 int pipe_fd;
513 struct tevent_context *event_ctx;
514 struct messaging_context *msg_ctx;
515 void (*post_cache_fill_fn)(struct tevent_context *,
516 struct messaging_context *);
519 static void cups_async_callback(struct tevent_context *event_ctx,
520 struct tevent_fd *event,
521 uint16_t flags,
522 void *p)
524 TALLOC_CTX *frame = talloc_stackframe();
525 struct cups_async_cb_args *cb_args = (struct cups_async_cb_args *)p;
526 struct pcap_cache *tmp_pcap_cache = NULL;
527 bool ret_ok;
528 struct pcap_data pcap_data;
529 DATA_BLOB pcap_blob;
530 enum ndr_err_code ndr_ret;
531 uint32_t i;
533 DEBUG(5,("cups_async_callback: callback received for printer data. "
534 "fd = %d\n", cb_args->pipe_fd));
536 ret_ok = recv_pcap_blob(frame, cb_args->pipe_fd, &pcap_blob);
537 if (!ret_ok) {
538 DEBUG(0,("failed to recv pcap blob\n"));
539 goto err_out;
542 ndr_ret = ndr_pull_struct_blob(&pcap_blob, frame, &pcap_data,
543 (ndr_pull_flags_fn_t)ndr_pull_pcap_data);
544 if (ndr_ret != NDR_ERR_SUCCESS) {
545 goto err_out;
548 if (!NT_STATUS_IS_OK(pcap_data.status)) {
549 DEBUG(3,("failed to retrieve printer list: %s\n",
550 nt_errstr(pcap_data.status)));
551 goto err_out;
554 for (i = 0; i < pcap_data.count; i++) {
555 ret_ok = pcap_cache_add_specific(&tmp_pcap_cache,
556 pcap_data.printers[i].name,
557 pcap_data.printers[i].info,
558 pcap_data.printers[i].location);
559 if (!ret_ok) {
560 DEBUG(0, ("failed to add to tmp pcap cache\n"));
561 goto err_out;
565 /* replace the system-wide pcap cache with a (possibly empty) new one */
566 ret_ok = pcap_cache_replace(tmp_pcap_cache);
567 if (!ret_ok) {
568 DEBUG(0, ("failed to replace pcap cache\n"));
569 } else if (cb_args->post_cache_fill_fn != NULL) {
570 /* Caller requested post cache fill callback */
571 cb_args->post_cache_fill_fn(cb_args->event_ctx,
572 cb_args->msg_ctx);
574 err_out:
575 pcap_cache_destroy_specific(&tmp_pcap_cache);
576 TALLOC_FREE(frame);
577 TALLOC_FREE(cache_fd_event);
578 close(cb_args->pipe_fd);
579 TALLOC_FREE(cb_args);
582 bool cups_cache_reload(struct tevent_context *ev,
583 struct messaging_context *msg_ctx,
584 void (*post_cache_fill_fn)(struct tevent_context *,
585 struct messaging_context *))
587 struct cups_async_cb_args *cb_args;
588 int *p_pipe_fd;
590 cb_args = talloc(NULL, struct cups_async_cb_args);
591 if (cb_args == NULL) {
592 return false;
595 cb_args->post_cache_fill_fn = post_cache_fill_fn;
596 cb_args->event_ctx = ev;
597 cb_args->msg_ctx = msg_ctx;
598 p_pipe_fd = &cb_args->pipe_fd;
599 *p_pipe_fd = -1;
601 /* Set up an async refresh. */
602 if (!cups_pcap_load_async(ev, msg_ctx, p_pipe_fd)) {
603 talloc_free(cb_args);
604 return false;
607 DEBUG(10,("cups_cache_reload: async read on fd %d\n",
608 *p_pipe_fd ));
610 /* Trigger an event when the pipe can be read. */
611 cache_fd_event = tevent_add_fd(ev,
612 NULL, *p_pipe_fd,
613 TEVENT_FD_READ,
614 cups_async_callback,
615 (void *)cb_args);
616 if (!cache_fd_event) {
617 close(*p_pipe_fd);
618 TALLOC_FREE(cb_args);
619 return false;
622 return true;
626 * 'cups_job_delete()' - Delete a job.
629 static int cups_job_delete(const char *sharename, const char *lprm_command, struct printjob *pjob)
631 TALLOC_CTX *frame = talloc_stackframe();
632 int ret = 1; /* Return value */
633 http_t *http = NULL; /* HTTP connection to server */
634 ipp_t *request = NULL, /* IPP Request */
635 *response = NULL; /* IPP Response */
636 cups_lang_t *language = NULL; /* Default language */
637 char *user = NULL;
638 char uri[HTTP_MAX_URI] = {0}; /* printer-uri attribute */
639 http_uri_status_t ustatus;
640 size_t size;
642 DEBUG(5,("cups_job_delete(%s, %p (%d))\n", sharename, pjob, pjob->sysjob));
645 * Make sure we don't ask for passwords...
648 cupsSetPasswordCB(cups_passwd_cb);
651 * Try to connect to the server...
654 if ((http = cups_connect(frame)) == NULL) {
655 goto out;
659 * Build an IPP_CANCEL_JOB request, which requires the following
660 * attributes:
662 * attributes-charset
663 * attributes-natural-language
664 * job-uri
665 * requesting-user-name
668 request = ippNew();
670 ippSetOperation(request, IPP_CANCEL_JOB);
671 ippSetRequestId(request, 1);
673 language = cupsLangDefault();
675 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
676 "attributes-charset", NULL, "utf-8");
678 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
679 "attributes-natural-language", NULL, language->language);
681 ustatus = httpAssembleURIf(HTTP_URI_CODING_ALL,
682 uri,
683 sizeof(uri),
684 "ipp",
685 NULL, /* username */
686 "localhost",
687 ippPort(),
688 "/jobs/%d",
689 pjob->sysjob);
690 if (ustatus != HTTP_URI_STATUS_OK) {
691 goto out;
694 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri);
696 if (!push_utf8_talloc(frame, &user, pjob->user, &size)) {
697 goto out;
700 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
701 NULL, user);
704 * Do the request and get back a response...
707 if ((response = cupsDoRequest(http, request, "/jobs")) != NULL) {
708 if (ippGetStatusCode(response) >= IPP_OK_CONFLICT) {
709 DEBUG(0,("Unable to cancel job %d - %s\n", pjob->sysjob,
710 ippErrorString(cupsLastError())));
711 } else {
712 ret = 0;
714 } else {
715 DEBUG(0,("Unable to cancel job %d - %s\n", pjob->sysjob,
716 ippErrorString(cupsLastError())));
719 out:
720 if (response)
721 ippDelete(response);
723 if (language)
724 cupsLangFree(language);
726 if (http)
727 httpClose(http);
729 TALLOC_FREE(frame);
730 return ret;
735 * 'cups_job_pause()' - Pause a job.
738 static int cups_job_pause(int snum, struct printjob *pjob)
740 TALLOC_CTX *frame = talloc_stackframe();
741 int ret = 1; /* Return value */
742 http_t *http = NULL; /* HTTP connection to server */
743 ipp_t *request = NULL, /* IPP Request */
744 *response = NULL; /* IPP Response */
745 cups_lang_t *language = NULL; /* Default language */
746 char *user = NULL;
747 char uri[HTTP_MAX_URI] = {0}; /* printer-uri attribute */
748 http_uri_status_t ustatus;
749 size_t size;
751 DEBUG(5,("cups_job_pause(%d, %p (%d))\n", snum, pjob, pjob->sysjob));
754 * Make sure we don't ask for passwords...
757 cupsSetPasswordCB(cups_passwd_cb);
760 * Try to connect to the server...
763 if ((http = cups_connect(frame)) == NULL) {
764 goto out;
768 * Build an IPP_HOLD_JOB request, which requires the following
769 * attributes:
771 * attributes-charset
772 * attributes-natural-language
773 * job-uri
774 * requesting-user-name
777 request = ippNew();
779 ippSetOperation(request, IPP_HOLD_JOB);
780 ippSetRequestId(request, 1);
782 language = cupsLangDefault();
784 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
785 "attributes-charset", NULL, "utf-8");
787 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
788 "attributes-natural-language", NULL, language->language);
790 ustatus = httpAssembleURIf(HTTP_URI_CODING_ALL,
791 uri,
792 sizeof(uri),
793 "ipp",
794 NULL, /* username */
795 "localhost",
796 ippPort(),
797 "/jobs/%d",
798 pjob->sysjob);
799 if (ustatus != HTTP_URI_STATUS_OK) {
800 goto out;
803 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri);
805 if (!push_utf8_talloc(frame, &user, pjob->user, &size)) {
806 goto out;
808 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
809 NULL, user);
812 * Do the request and get back a response...
815 if ((response = cupsDoRequest(http, request, "/jobs")) != NULL) {
816 if (ippGetStatusCode(response) >= IPP_OK_CONFLICT) {
817 DEBUG(0,("Unable to hold job %d - %s\n", pjob->sysjob,
818 ippErrorString(cupsLastError())));
819 } else {
820 ret = 0;
822 } else {
823 DEBUG(0,("Unable to hold job %d - %s\n", pjob->sysjob,
824 ippErrorString(cupsLastError())));
827 out:
828 if (response)
829 ippDelete(response);
831 if (language)
832 cupsLangFree(language);
834 if (http)
835 httpClose(http);
837 TALLOC_FREE(frame);
838 return ret;
843 * 'cups_job_resume()' - Resume a paused job.
846 static int cups_job_resume(int snum, struct printjob *pjob)
848 TALLOC_CTX *frame = talloc_stackframe();
849 int ret = 1; /* Return value */
850 http_t *http = NULL; /* HTTP connection to server */
851 ipp_t *request = NULL, /* IPP Request */
852 *response = NULL; /* IPP Response */
853 cups_lang_t *language = NULL; /* Default language */
854 char *user = NULL;
855 char uri[HTTP_MAX_URI] = {0}; /* printer-uri attribute */
856 http_uri_status_t ustatus;
857 size_t size;
859 DEBUG(5,("cups_job_resume(%d, %p (%d))\n", snum, pjob, pjob->sysjob));
862 * Make sure we don't ask for passwords...
865 cupsSetPasswordCB(cups_passwd_cb);
868 * Try to connect to the server...
871 if ((http = cups_connect(frame)) == NULL) {
872 goto out;
876 * Build an IPP_RELEASE_JOB request, which requires the following
877 * attributes:
879 * attributes-charset
880 * attributes-natural-language
881 * job-uri
882 * requesting-user-name
885 request = ippNew();
887 ippSetOperation(request, IPP_RELEASE_JOB);
888 ippSetRequestId(request, 1);
890 language = cupsLangDefault();
892 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
893 "attributes-charset", NULL, "utf-8");
895 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
896 "attributes-natural-language", NULL, language->language);
898 ustatus = httpAssembleURIf(HTTP_URI_CODING_ALL,
899 uri,
900 sizeof(uri),
901 "ipp",
902 NULL, /* username */
903 "localhost",
904 ippPort(),
905 "/jobs/%d",
906 pjob->sysjob);
907 if (ustatus != HTTP_URI_STATUS_OK) {
908 goto out;
911 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri);
913 if (!push_utf8_talloc(frame, &user, pjob->user, &size)) {
914 goto out;
916 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
917 NULL, user);
920 * Do the request and get back a response...
923 if ((response = cupsDoRequest(http, request, "/jobs")) != NULL) {
924 if (ippGetStatusCode(response) >= IPP_OK_CONFLICT) {
925 DEBUG(0,("Unable to release job %d - %s\n", pjob->sysjob,
926 ippErrorString(cupsLastError())));
927 } else {
928 ret = 0;
930 } else {
931 DEBUG(0,("Unable to release job %d - %s\n", pjob->sysjob,
932 ippErrorString(cupsLastError())));
935 out:
936 if (response)
937 ippDelete(response);
939 if (language)
940 cupsLangFree(language);
942 if (http)
943 httpClose(http);
945 TALLOC_FREE(frame);
946 return ret;
951 * 'cups_job_submit()' - Submit a job for printing.
954 static int cups_job_submit(int snum, struct printjob *pjob,
955 enum printing_types printing_type,
956 char *lpq_cmd)
958 TALLOC_CTX *frame = talloc_stackframe();
959 const struct loadparm_substitution *lp_sub =
960 loadparm_s3_global_substitution();
961 int ret = 1; /* Return value */
962 http_t *http = NULL; /* HTTP connection to server */
963 ipp_t *request = NULL, /* IPP Request */
964 *response = NULL; /* IPP Response */
965 ipp_attribute_t *attr_job_id = NULL; /* IPP Attribute "job-id" */
966 cups_lang_t *language = NULL; /* Default language */
967 char uri[HTTP_MAX_URI] = {0}; /* printer-uri attribute */
968 http_uri_status_t ustatus;
969 char *new_jobname = NULL;
970 int num_options = 0;
971 cups_option_t *options = NULL;
972 char *printername = NULL;
973 char *user = NULL;
974 char *jobname = NULL;
975 char *cupsoptions = NULL;
976 char *filename = NULL;
977 size_t size;
979 DEBUG(5,("cups_job_submit(%d, %p)\n", snum, pjob));
982 * Make sure we don't ask for passwords...
985 cupsSetPasswordCB(cups_passwd_cb);
988 * Try to connect to the server...
991 if ((http = cups_connect(frame)) == NULL) {
992 goto out;
996 * Build an IPP_PRINT_JOB request, which requires the following
997 * attributes:
999 * attributes-charset
1000 * attributes-natural-language
1001 * printer-uri
1002 * requesting-user-name
1003 * [document-data]
1006 request = ippNew();
1008 ippSetOperation(request, IPP_PRINT_JOB);
1009 ippSetRequestId(request, 1);
1011 language = cupsLangDefault();
1013 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1014 "attributes-charset", NULL, "utf-8");
1016 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1017 "attributes-natural-language", NULL, language->language);
1019 if (!push_utf8_talloc(frame, &printername,
1020 lp_printername(talloc_tos(), lp_sub, snum),
1021 &size)) {
1022 goto out;
1024 ustatus = httpAssembleURIf(HTTP_URI_CODING_ALL,
1025 uri,
1026 sizeof(uri),
1027 "ipp",
1028 NULL, /* username */
1029 "localhost",
1030 ippPort(),
1031 "/printers/%s",
1032 printername);
1033 if (ustatus != HTTP_URI_STATUS_OK) {
1034 goto out;
1037 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1038 "printer-uri", NULL, uri);
1040 if (!push_utf8_talloc(frame, &user, pjob->user, &size)) {
1041 goto out;
1043 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
1044 NULL, user);
1046 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1047 "job-originating-host-name", NULL,
1048 pjob->clientmachine);
1050 if (!push_utf8_talloc(frame, &jobname, pjob->jobname, &size)) {
1051 goto out;
1053 new_jobname = talloc_asprintf(frame,
1054 "%s%.8u %s", PRINT_SPOOL_PREFIX,
1055 pjob->jobid, jobname);
1056 if (new_jobname == NULL) {
1057 goto out;
1060 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name", NULL,
1061 new_jobname);
1064 * add any options defined in smb.conf
1067 if (!push_utf8_talloc(frame, &cupsoptions,
1068 lp_cups_options(talloc_tos(), lp_sub, snum), &size)) {
1069 goto out;
1071 num_options = 0;
1072 options = NULL;
1073 num_options = cupsParseOptions(cupsoptions, num_options, &options);
1075 if ( num_options )
1076 cupsEncodeOptions(request, num_options, options);
1079 * Do the request and get back a response...
1082 ustatus = httpAssembleURIf(HTTP_URI_CODING_ALL,
1083 uri,
1084 sizeof(uri),
1085 "ipp",
1086 NULL, /* username */
1087 "localhost",
1088 ippPort(),
1089 "/printers/%s",
1090 printername);
1091 if (ustatus != HTTP_URI_STATUS_OK) {
1092 goto out;
1095 if (!push_utf8_talloc(frame, &filename, pjob->filename, &size)) {
1096 goto out;
1098 if ((response = cupsDoFileRequest(http, request, uri, pjob->filename)) != NULL) {
1099 if (ippGetStatusCode(response) >= IPP_OK_CONFLICT) {
1100 DEBUG(0,("Unable to print file to %s - %s\n",
1101 lp_printername(talloc_tos(), lp_sub, snum),
1102 ippErrorString(cupsLastError())));
1103 } else {
1104 ret = 0;
1105 attr_job_id = ippFindAttribute(response, "job-id", IPP_TAG_INTEGER);
1106 if(attr_job_id) {
1107 pjob->sysjob = ippGetInteger(attr_job_id, 0);
1108 DEBUG(5,("cups_job_submit: job-id %d\n", pjob->sysjob));
1109 } else {
1110 DEBUG(0,("Missing job-id attribute in IPP response\n"));
1113 } else {
1114 DEBUG(0,("Unable to print file to `%s' - %s\n",
1115 lp_printername(talloc_tos(), lp_sub, snum),
1116 ippErrorString(cupsLastError())));
1119 if ( ret == 0 )
1120 unlink(pjob->filename);
1121 /* else print_job_end will do it for us */
1123 out:
1124 if (response)
1125 ippDelete(response);
1127 if (language)
1128 cupsLangFree(language);
1130 if (http)
1131 httpClose(http);
1133 TALLOC_FREE(frame);
1135 return ret;
1139 * 'cups_queue_get()' - Get all the jobs in the print queue.
1142 static int cups_queue_get(const char *sharename,
1143 enum printing_types printing_type,
1144 char *lpq_command,
1145 print_queue_struct **q,
1146 print_status_struct *status)
1148 TALLOC_CTX *frame = talloc_stackframe();
1149 char *printername = NULL;
1150 http_t *http = NULL; /* HTTP connection to server */
1151 ipp_t *request = NULL, /* IPP Request */
1152 *response = NULL; /* IPP Response */
1153 ipp_attribute_t *attr = NULL; /* Current attribute */
1154 cups_lang_t *language = NULL; /* Default language */
1155 char uri[HTTP_MAX_URI] = {0}; /* printer-uri attribute */
1156 http_uri_status_t ustatus;
1157 int qcount = 0, /* Number of active queue entries */
1158 qalloc = 0; /* Number of queue entries allocated */
1159 print_queue_struct *queue = NULL, /* Queue entries */
1160 *temp; /* Temporary pointer for queue */
1161 char *user_name = NULL, /* job-originating-user-name attribute */
1162 *job_name = NULL; /* job-name attribute */
1163 int job_id; /* job-id attribute */
1164 int job_k_octets; /* job-k-octets attribute */
1165 time_t job_time; /* time-at-creation attribute */
1166 ipp_jstate_t job_status; /* job-status attribute */
1167 int job_priority; /* job-priority attribute */
1168 size_t size;
1169 static const char *jattrs[] = /* Requested job attributes */
1171 "job-id",
1172 "job-k-octets",
1173 "job-name",
1174 "job-originating-user-name",
1175 "job-priority",
1176 "job-state",
1177 "time-at-creation",
1179 static const char *pattrs[] = /* Requested printer attributes */
1181 "printer-state",
1182 "printer-state-message"
1185 *q = NULL;
1187 /* HACK ALERT!!! The problem with support the 'printer name'
1188 option is that we key the tdb off the sharename. So we will
1189 overload the lpq_command string to pass in the printername
1190 (which is basically what we do for non-cups printers ... using
1191 the lpq_command to get the queue listing). */
1193 if (!push_utf8_talloc(frame, &printername, lpq_command, &size)) {
1194 goto out;
1196 DEBUG(5,("cups_queue_get(%s, %p, %p)\n", lpq_command, q, status));
1199 * Make sure we don't ask for passwords...
1202 cupsSetPasswordCB(cups_passwd_cb);
1205 * Try to connect to the server...
1208 if ((http = cups_connect(frame)) == NULL) {
1209 goto out;
1213 * Generate the printer URI...
1216 ustatus = httpAssembleURIf(HTTP_URI_CODING_ALL,
1217 uri,
1218 sizeof(uri),
1219 "ipp",
1220 NULL, /* username */
1221 "localhost",
1222 ippPort(),
1223 "/printers/%s",
1224 printername);
1225 if (ustatus != HTTP_URI_STATUS_OK) {
1226 goto out;
1230 * Build an IPP_GET_JOBS request, which requires the following
1231 * attributes:
1233 * attributes-charset
1234 * attributes-natural-language
1235 * requested-attributes
1236 * printer-uri
1239 request = ippNew();
1241 ippSetOperation(request, IPP_GET_JOBS);
1242 ippSetRequestId(request, 1);
1244 language = cupsLangDefault();
1246 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1247 "attributes-charset", NULL, "utf-8");
1249 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1250 "attributes-natural-language", NULL, language->language);
1252 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
1253 "requested-attributes",
1254 (sizeof(jattrs) / sizeof(jattrs[0])),
1255 NULL, jattrs);
1257 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1258 "printer-uri", NULL, uri);
1261 * Do the request and get back a response...
1264 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
1265 DEBUG(0,("Unable to get jobs for %s - %s\n", uri,
1266 ippErrorString(cupsLastError())));
1267 goto out;
1270 if (ippGetStatusCode(response) >= IPP_OK_CONFLICT) {
1271 DEBUG(0,("Unable to get jobs for %s - %s\n", uri,
1272 ippErrorString(ippGetStatusCode(response))));
1273 goto out;
1277 * Process the jobs...
1280 qcount = 0;
1281 qalloc = 0;
1282 queue = NULL;
1284 for (attr = ippFirstAttribute(response); attr != NULL; attr = ippNextAttribute(response)) {
1286 * Skip leading attributes until we hit a job...
1289 while (attr != NULL && ippGetGroupTag(attr) != IPP_TAG_JOB)
1290 attr = ippNextAttribute(response);
1292 if (attr == NULL)
1293 break;
1296 * Allocate memory as needed...
1298 if (qcount >= qalloc) {
1299 qalloc += 16;
1301 queue = SMB_REALLOC_ARRAY(queue, print_queue_struct, qalloc);
1303 if (queue == NULL) {
1304 DEBUG(0,("cups_queue_get: Not enough memory!\n"));
1305 qcount = 0;
1306 goto out;
1310 temp = queue + qcount;
1311 memset(temp, 0, sizeof(print_queue_struct));
1314 * Pull the needed attributes from this job...
1317 job_id = 0;
1318 job_priority = 50;
1319 job_status = IPP_JOB_PENDING;
1320 job_time = 0;
1321 job_k_octets = 0;
1322 user_name = NULL;
1323 job_name = NULL;
1325 while (attr != NULL && ippGetGroupTag(attr) == IPP_TAG_JOB) {
1326 if (ippGetName(attr) == NULL) {
1327 attr = ippNextAttribute(response);
1328 break;
1331 if (strcmp(ippGetName(attr), "job-id") == 0 &&
1332 ippGetValueTag(attr) == IPP_TAG_INTEGER)
1333 job_id = ippGetInteger(attr, 0);
1335 if (strcmp(ippGetName(attr), "job-k-octets") == 0 &&
1336 ippGetValueTag(attr) == IPP_TAG_INTEGER)
1337 job_k_octets = ippGetInteger(attr, 0);
1339 if (strcmp(ippGetName(attr), "job-priority") == 0 &&
1340 ippGetValueTag(attr) == IPP_TAG_INTEGER)
1341 job_priority = ippGetInteger(attr, 0);
1343 if (strcmp(ippGetName(attr), "job-state") == 0 &&
1344 ippGetValueTag(attr) == IPP_TAG_ENUM)
1345 job_status = (ipp_jstate_t)ippGetInteger(attr, 0);
1347 if (strcmp(ippGetName(attr), "time-at-creation") == 0 &&
1348 ippGetValueTag(attr) == IPP_TAG_INTEGER)
1349 job_time = ippGetInteger(attr, 0);
1351 if (strcmp(ippGetName(attr), "job-name") == 0 &&
1352 ippGetValueTag(attr) == IPP_TAG_NAME) {
1353 if (!pull_utf8_talloc(frame,
1354 &job_name,
1355 ippGetString(attr, 0, NULL),
1356 &size)) {
1357 goto out;
1361 if (strcmp(ippGetName(attr), "job-originating-user-name") == 0 &&
1362 ippGetValueTag(attr) == IPP_TAG_NAME) {
1363 if (!pull_utf8_talloc(frame,
1364 &user_name,
1365 ippGetString(attr, 0, NULL),
1366 &size)) {
1367 goto out;
1371 attr = ippNextAttribute(response);
1375 * See if we have everything needed...
1378 if (user_name == NULL || job_name == NULL || job_id == 0) {
1379 if (attr == NULL)
1380 break;
1381 else
1382 continue;
1385 temp->sysjob = job_id;
1386 temp->size = job_k_octets * 1024;
1387 temp->status = job_status == IPP_JOB_PENDING ? LPQ_QUEUED :
1388 job_status == IPP_JOB_STOPPED ? LPQ_PAUSED :
1389 job_status == IPP_JOB_HELD ? LPQ_PAUSED :
1390 LPQ_PRINTING;
1391 temp->priority = job_priority;
1392 temp->time = job_time;
1393 strlcpy(temp->fs_user, user_name, sizeof(temp->fs_user));
1394 strlcpy(temp->fs_file, job_name, sizeof(temp->fs_file));
1396 qcount ++;
1398 if (attr == NULL)
1399 break;
1402 ippDelete(response);
1403 response = NULL;
1406 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the
1407 * following attributes:
1409 * attributes-charset
1410 * attributes-natural-language
1411 * requested-attributes
1412 * printer-uri
1415 request = ippNew();
1417 ippSetOperation(request, IPP_GET_PRINTER_ATTRIBUTES);
1418 ippSetRequestId(request, 1);
1420 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1421 "attributes-charset", NULL, "utf-8");
1423 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1424 "attributes-natural-language", NULL, language->language);
1426 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1427 "requested-attributes",
1428 (sizeof(pattrs) / sizeof(pattrs[0])),
1429 NULL, pattrs);
1431 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1432 "printer-uri", NULL, uri);
1435 * Do the request and get back a response...
1438 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
1439 DEBUG(0,("Unable to get printer status for %s - %s\n", printername,
1440 ippErrorString(cupsLastError())));
1441 goto out;
1444 if (ippGetStatusCode(response) >= IPP_OK_CONFLICT) {
1445 DEBUG(0,("Unable to get printer status for %s - %s\n", printername,
1446 ippErrorString(ippGetStatusCode(response))));
1447 goto out;
1451 * Get the current printer status and convert it to the SAMBA values.
1454 if ((attr = ippFindAttribute(response, "printer-state", IPP_TAG_ENUM)) != NULL) {
1455 if (ippGetInteger(attr, 0) == IPP_PRINTER_STOPPED)
1456 status->status = LPSTAT_STOPPED;
1457 else
1458 status->status = LPSTAT_OK;
1461 if ((attr = ippFindAttribute(response, "printer-state-message",
1462 IPP_TAG_TEXT)) != NULL) {
1463 char *msg = NULL;
1464 if (!pull_utf8_talloc(frame, &msg,
1465 ippGetString(attr, 0, NULL),
1466 &size)) {
1467 SAFE_FREE(queue);
1468 qcount = 0;
1469 goto out;
1471 fstrcpy(status->message, msg);
1474 out:
1477 * Return the job queue...
1480 *q = queue;
1482 if (response)
1483 ippDelete(response);
1485 if (language)
1486 cupsLangFree(language);
1488 if (http)
1489 httpClose(http);
1491 TALLOC_FREE(frame);
1492 return qcount;
1497 * 'cups_queue_pause()' - Pause a print queue.
1500 static int cups_queue_pause(int snum)
1502 TALLOC_CTX *frame = talloc_stackframe();
1503 const struct loadparm_substitution *lp_sub =
1504 loadparm_s3_global_substitution();
1505 int ret = 1; /* Return value */
1506 http_t *http = NULL; /* HTTP connection to server */
1507 ipp_t *request = NULL, /* IPP Request */
1508 *response = NULL; /* IPP Response */
1509 cups_lang_t *language = NULL; /* Default language */
1510 char *printername = NULL;
1511 char *username = NULL;
1512 char uri[HTTP_MAX_URI] = {0}; /* printer-uri attribute */
1513 http_uri_status_t ustatus;
1514 size_t size;
1516 DEBUG(5,("cups_queue_pause(%d)\n", snum));
1519 * Make sure we don't ask for passwords...
1522 cupsSetPasswordCB(cups_passwd_cb);
1525 * Try to connect to the server...
1528 if ((http = cups_connect(frame)) == NULL) {
1529 goto out;
1533 * Build an IPP_PAUSE_PRINTER request, which requires the following
1534 * attributes:
1536 * attributes-charset
1537 * attributes-natural-language
1538 * printer-uri
1539 * requesting-user-name
1542 request = ippNew();
1544 ippSetOperation(request, IPP_PAUSE_PRINTER);
1545 ippSetRequestId(request, 1);
1547 language = cupsLangDefault();
1549 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1550 "attributes-charset", NULL, "utf-8");
1552 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1553 "attributes-natural-language", NULL, language->language);
1555 if (!push_utf8_talloc(frame, &printername,
1556 lp_printername(talloc_tos(), lp_sub, snum), &size)) {
1557 goto out;
1559 ustatus = httpAssembleURIf(HTTP_URI_CODING_ALL,
1560 uri,
1561 sizeof(uri),
1562 "ipp",
1563 NULL, /* username */
1564 "localhost",
1565 ippPort(),
1566 "/printers/%s",
1567 printername);
1568 if (ustatus != HTTP_URI_STATUS_OK) {
1569 goto out;
1572 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
1574 if (!push_utf8_talloc(frame, &username, current_user_info.unix_name, &size)) {
1575 goto out;
1577 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
1578 NULL, username);
1581 * Do the request and get back a response...
1584 if ((response = cupsDoRequest(http, request, "/admin/")) != NULL) {
1585 if (ippGetStatusCode(response) >= IPP_OK_CONFLICT) {
1586 DEBUG(0,("Unable to pause printer %s - %s\n",
1587 lp_printername(talloc_tos(), lp_sub, snum),
1588 ippErrorString(cupsLastError())));
1589 } else {
1590 ret = 0;
1592 } else {
1593 DEBUG(0,("Unable to pause printer %s - %s\n",
1594 lp_printername(talloc_tos(), lp_sub, snum),
1595 ippErrorString(cupsLastError())));
1598 out:
1599 if (response)
1600 ippDelete(response);
1602 if (language)
1603 cupsLangFree(language);
1605 if (http)
1606 httpClose(http);
1608 TALLOC_FREE(frame);
1609 return ret;
1614 * 'cups_queue_resume()' - Restart a print queue.
1617 static int cups_queue_resume(int snum)
1619 TALLOC_CTX *frame = talloc_stackframe();
1620 const struct loadparm_substitution *lp_sub =
1621 loadparm_s3_global_substitution();
1622 int ret = 1; /* Return value */
1623 http_t *http = NULL; /* HTTP connection to server */
1624 ipp_t *request = NULL, /* IPP Request */
1625 *response = NULL; /* IPP Response */
1626 cups_lang_t *language = NULL; /* Default language */
1627 char *printername = NULL;
1628 char *username = NULL;
1629 char uri[HTTP_MAX_URI] = {0}; /* printer-uri attribute */
1630 http_uri_status_t ustatus;
1631 size_t size;
1633 DEBUG(5,("cups_queue_resume(%d)\n", snum));
1636 * Make sure we don't ask for passwords...
1639 cupsSetPasswordCB(cups_passwd_cb);
1642 * Try to connect to the server...
1645 if ((http = cups_connect(frame)) == NULL) {
1646 goto out;
1650 * Build an IPP_RESUME_PRINTER request, which requires the following
1651 * attributes:
1653 * attributes-charset
1654 * attributes-natural-language
1655 * printer-uri
1656 * requesting-user-name
1659 request = ippNew();
1661 ippSetOperation(request, IPP_RESUME_PRINTER);
1662 ippSetRequestId(request, 1);
1664 language = cupsLangDefault();
1666 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1667 "attributes-charset", NULL, "utf-8");
1669 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1670 "attributes-natural-language", NULL, language->language);
1672 if (!push_utf8_talloc(frame, &printername, lp_printername(talloc_tos(), lp_sub, snum),
1673 &size)) {
1674 goto out;
1676 ustatus = httpAssembleURIf(HTTP_URI_CODING_ALL,
1677 uri,
1678 sizeof(uri),
1679 "ipp",
1680 NULL, /* username */
1681 "localhost",
1682 ippPort(),
1683 "/printers/%s",
1684 printername);
1685 if (ustatus != HTTP_URI_STATUS_OK) {
1686 goto out;
1689 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
1691 if (!push_utf8_talloc(frame, &username, current_user_info.unix_name, &size)) {
1692 goto out;
1694 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
1695 NULL, username);
1698 * Do the request and get back a response...
1701 if ((response = cupsDoRequest(http, request, "/admin/")) != NULL) {
1702 if (ippGetStatusCode(response) >= IPP_OK_CONFLICT) {
1703 DEBUG(0,("Unable to resume printer %s - %s\n",
1704 lp_printername(talloc_tos(), lp_sub, snum),
1705 ippErrorString(cupsLastError())));
1706 } else {
1707 ret = 0;
1709 } else {
1710 DEBUG(0,("Unable to resume printer %s - %s\n",
1711 lp_printername(talloc_tos(), lp_sub, snum),
1712 ippErrorString(cupsLastError())));
1715 out:
1716 if (response)
1717 ippDelete(response);
1719 if (language)
1720 cupsLangFree(language);
1722 if (http)
1723 httpClose(http);
1725 TALLOC_FREE(frame);
1726 return ret;
1729 /*******************************************************************
1730 * CUPS printing interface definitions...
1731 ******************************************************************/
1733 struct printif cups_printif =
1735 PRINT_CUPS,
1736 cups_queue_get,
1737 cups_queue_pause,
1738 cups_queue_resume,
1739 cups_job_delete,
1740 cups_job_pause,
1741 cups_job_resume,
1742 cups_job_submit,
1745 #else
1746 /* this keeps fussy compilers happy */
1747 void print_cups_dummy(void);
1748 void print_cups_dummy(void) {}
1749 #endif /* HAVE_CUPS */