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.
27 #include "printing/pcap.h"
28 #include "librpc/gen_ndr/ndr_printcap.h"
31 #include <cups/cups.h>
32 #include <cups/language.h>
34 static SIG_ATOMIC_T gotalarm;
36 /***************************************************************
37 Signal function to tell us we timed out.
38 ****************************************************************/
40 static void gotalarm_sig(int signum)
45 extern userdom_struct current_user_info;
48 * 'cups_passwd_cb()' - The CUPS password callback...
51 static const char * /* O - Password or NULL */
52 cups_passwd_cb(const char *prompt) /* I - Prompt */
55 * Always return NULL to indicate that no password is available...
61 static http_t *cups_connect(TALLOC_CTX *frame)
64 char *server = NULL, *p = NULL;
66 int timeout = lp_cups_connection_timeout();
69 if (lp_cups_server() != NULL && strlen(lp_cups_server()) > 0) {
70 if (!push_utf8_talloc(frame, &server, lp_cups_server(), &size)) {
74 server = talloc_strdup(frame,cupsServer());
80 p = strchr(server, ':');
88 DEBUG(10, ("connecting to cups server %s:%d\n",
94 CatchSignal(SIGALRM, gotalarm_sig);
98 #ifdef HAVE_HTTPCONNECTENCRYPT
99 http = httpConnectEncrypt(server, port, lp_cups_encrypt());
101 http = httpConnect(server, port);
105 CatchSignal(SIGALRM, SIG_IGN);
109 DEBUG(0,("Unable to connect to CUPS server %s:%d - %s\n",
110 server, port, strerror(errno)));
116 static bool send_pcap_blob(DATA_BLOB *pcap_blob, int fd)
120 ret = sys_write(fd, &pcap_blob->length, sizeof(pcap_blob->length));
121 if (ret != sizeof(pcap_blob->length)) {
125 ret = sys_write(fd, pcap_blob->data, pcap_blob->length);
126 if (ret != pcap_blob->length) {
130 DEBUG(10, ("successfully sent blob of len %ld\n", (int64_t)ret));
134 static bool recv_pcap_blob(TALLOC_CTX *mem_ctx, int fd, DATA_BLOB *pcap_blob)
139 ret = sys_read(fd, &blob_len, sizeof(blob_len));
140 if (ret != sizeof(blob_len)) {
144 *pcap_blob = data_blob_talloc_named(mem_ctx, NULL, blob_len,
146 if (pcap_blob->length != blob_len) {
149 ret = sys_read(fd, pcap_blob->data, blob_len);
150 if (ret != blob_len) {
151 talloc_free(pcap_blob->data);
155 DEBUG(10, ("successfully recvd blob of len %ld\n", (int64_t)ret));
159 static bool cups_cache_reload_async(int fd)
161 TALLOC_CTX *frame = talloc_stackframe();
162 struct pcap_data pcap_data;
163 struct pcap_printer *printer;
164 http_t *http = NULL; /* HTTP connection to server */
165 ipp_t *request = NULL, /* IPP Request */
166 *response = NULL; /* IPP Response */
167 ipp_attribute_t *attr; /* Current attribute */
168 cups_lang_t *language = NULL; /* Default language */
169 char *name, /* printer-name attribute */
170 *info; /* printer-info attribute */
171 static const char *requested[] =/* Requested attributes */
178 enum ndr_err_code ndr_ret;
181 ZERO_STRUCT(pcap_data);
182 pcap_data.status = NT_STATUS_UNSUCCESSFUL;
184 DEBUG(5, ("reloading cups printcap cache\n"));
187 * Make sure we don't ask for passwords...
190 cupsSetPasswordCB(cups_passwd_cb);
193 * Try to connect to the server...
196 if ((http = cups_connect(frame)) == NULL) {
201 * Build a CUPS_GET_PRINTERS request, which requires the following
205 * attributes-natural-language
206 * requested-attributes
211 request->request.op.operation_id = CUPS_GET_PRINTERS;
212 request->request.op.request_id = 1;
214 language = cupsLangDefault();
216 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
217 "attributes-charset", NULL, "utf-8");
219 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
220 "attributes-natural-language", NULL, language->language);
222 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
223 "requested-attributes",
224 (sizeof(requested) / sizeof(requested[0])),
228 * Do the request and get back a response...
231 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
232 DEBUG(0,("Unable to get printer list - %s\n",
233 ippErrorString(cupsLastError())));
237 for (attr = response->attrs; attr != NULL;) {
239 * Skip leading attributes until we hit a printer...
242 while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)
249 * Pull the needed attributes from this printer...
255 while (attr != NULL && attr->group_tag == IPP_TAG_PRINTER) {
256 if (strcmp(attr->name, "printer-name") == 0 &&
257 attr->value_tag == IPP_TAG_NAME) {
258 if (!pull_utf8_talloc(frame,
260 attr->values[0].string.text,
266 if (strcmp(attr->name, "printer-info") == 0 &&
267 attr->value_tag == IPP_TAG_TEXT) {
268 if (!pull_utf8_talloc(frame,
270 attr->values[0].string.text,
280 * See if we have everything needed...
286 if (pcap_data.count == 0) {
287 printer = talloc_array(frame, struct pcap_printer, 1);
289 printer = talloc_realloc(frame, pcap_data.printers,
291 pcap_data.count + 1);
293 if (printer == NULL) {
296 pcap_data.printers = printer;
297 pcap_data.printers[pcap_data.count].name = name;
298 pcap_data.printers[pcap_data.count].info = info;
306 * Build a CUPS_GET_CLASSES request, which requires the following
310 * attributes-natural-language
311 * requested-attributes
316 request->request.op.operation_id = CUPS_GET_CLASSES;
317 request->request.op.request_id = 1;
319 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
320 "attributes-charset", NULL, "utf-8");
322 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
323 "attributes-natural-language", NULL, language->language);
325 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
326 "requested-attributes",
327 (sizeof(requested) / sizeof(requested[0])),
331 * Do the request and get back a response...
334 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
335 DEBUG(0,("Unable to get printer list - %s\n",
336 ippErrorString(cupsLastError())));
340 for (attr = response->attrs; attr != NULL;) {
342 * Skip leading attributes until we hit a printer...
345 while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)
352 * Pull the needed attributes from this printer...
358 while (attr != NULL && attr->group_tag == IPP_TAG_PRINTER) {
359 if (strcmp(attr->name, "printer-name") == 0 &&
360 attr->value_tag == IPP_TAG_NAME) {
361 if (!pull_utf8_talloc(frame,
363 attr->values[0].string.text,
369 if (strcmp(attr->name, "printer-info") == 0 &&
370 attr->value_tag == IPP_TAG_TEXT) {
371 if (!pull_utf8_talloc(frame,
373 attr->values[0].string.text,
383 * See if we have everything needed...
389 if (pcap_data.count == 0) {
390 printer = talloc_array(frame, struct pcap_printer, 1);
392 printer = talloc_realloc(frame, pcap_data.printers,
394 pcap_data.count + 1);
396 if (printer == NULL) {
399 pcap_data.printers = printer;
400 pcap_data.printers[pcap_data.count].name = name;
401 pcap_data.printers[pcap_data.count].info = info;
406 pcap_data.status = NT_STATUS_OK;
412 cupsLangFree(language);
417 /* Send all the entries up the pipe. */
418 ndr_ret = ndr_push_struct_blob(&pcap_blob, frame, &pcap_data,
419 (ndr_push_flags_fn_t)ndr_push_pcap_data);
420 if (ndr_ret == NDR_ERR_SUCCESS) {
421 ret = send_pcap_blob(&pcap_blob, fd);
430 static struct fd_event *cache_fd_event;
432 static bool cups_pcap_load_async(struct tevent_context *ev,
433 struct messaging_context *msg_ctx,
442 if (cache_fd_event) {
443 DEBUG(3,("cups_pcap_load_async: already waiting for "
444 "a refresh event\n" ));
448 DEBUG(5,("cups_pcap_load_async: asynchronously loading cups printers\n"));
450 if (pipe(fds) == -1) {
455 if (pid == (pid_t)-1) {
456 DEBUG(10,("cups_pcap_load_async: fork failed %s\n",
464 DEBUG(10,("cups_pcap_load_async: child pid = %u\n",
465 (unsigned int)pid ));
474 close_all_print_db();
476 status = reinit_after_fork(msg_ctx, ev, procid_self(), true);
477 if (!NT_STATUS_IS_OK(status)) {
478 DEBUG(0,("cups_pcap_load_async: reinit_after_fork() failed\n"));
479 smb_panic("cups_pcap_load_async: reinit_after_fork() failed");
483 cups_cache_reload_async(fds[1]);
488 struct cups_async_cb_args {
490 struct event_context *event_ctx;
491 struct messaging_context *msg_ctx;
492 void (*post_cache_fill_fn)(struct event_context *,
493 struct messaging_context *);
496 static void cups_async_callback(struct event_context *event_ctx,
497 struct fd_event *event,
501 TALLOC_CTX *frame = talloc_stackframe();
502 struct cups_async_cb_args *cb_args = (struct cups_async_cb_args *)p;
503 struct pcap_cache *tmp_pcap_cache = NULL;
505 struct pcap_data pcap_data;
507 enum ndr_err_code ndr_ret;
510 DEBUG(5,("cups_async_callback: callback received for printer data. "
511 "fd = %d\n", cb_args->pipe_fd));
513 ret_ok = recv_pcap_blob(frame, cb_args->pipe_fd, &pcap_blob);
515 DEBUG(0,("failed to recv pcap blob\n"));
519 ndr_ret = ndr_pull_struct_blob(&pcap_blob, frame, &pcap_data,
520 (ndr_pull_flags_fn_t)ndr_pull_pcap_data);
521 if (ndr_ret != NDR_ERR_SUCCESS) {
525 if (!NT_STATUS_IS_OK(pcap_data.status)) {
526 DEBUG(0,("failed to retrieve printer list: %s\n",
527 nt_errstr(pcap_data.status)));
531 for (i = 0; i < pcap_data.count; i++) {
532 ret_ok = pcap_cache_add_specific(&tmp_pcap_cache,
533 pcap_data.printers[i].name,
534 pcap_data.printers[i].info);
536 DEBUG(0, ("failed to add to tmp pcap cache\n"));
542 DEBUG(0, ("failed to read a new printer list\n"));
543 pcap_cache_destroy_specific(&tmp_pcap_cache);
546 * replace the system-wide pcap cache with a (possibly empty)
549 ret_ok = pcap_cache_replace(tmp_pcap_cache);
551 DEBUG(0, ("failed to replace pcap cache\n"));
552 } else if (cb_args->post_cache_fill_fn != NULL) {
553 /* Caller requested post cache fill callback */
554 cb_args->post_cache_fill_fn(cb_args->event_ctx,
560 close(cb_args->pipe_fd);
561 TALLOC_FREE(cb_args);
562 TALLOC_FREE(cache_fd_event);
565 bool cups_cache_reload(struct tevent_context *ev,
566 struct messaging_context *msg_ctx,
567 void (*post_cache_fill_fn)(struct tevent_context *,
568 struct messaging_context *))
570 struct cups_async_cb_args *cb_args;
573 cb_args = TALLOC_P(NULL, struct cups_async_cb_args);
574 if (cb_args == NULL) {
578 cb_args->post_cache_fill_fn = post_cache_fill_fn;
579 cb_args->event_ctx = ev;
580 cb_args->msg_ctx = msg_ctx;
581 p_pipe_fd = &cb_args->pipe_fd;
584 /* Set up an async refresh. */
585 if (!cups_pcap_load_async(ev, msg_ctx, p_pipe_fd)) {
586 talloc_free(cb_args);
590 DEBUG(10,("cups_cache_reload: async read on fd %d\n",
593 /* Trigger an event when the pipe can be read. */
594 cache_fd_event = event_add_fd(ev,
599 if (!cache_fd_event) {
601 TALLOC_FREE(cb_args);
609 * 'cups_job_delete()' - Delete a job.
612 static int cups_job_delete(const char *sharename, const char *lprm_command, struct printjob *pjob)
614 TALLOC_CTX *frame = talloc_stackframe();
615 int ret = 1; /* Return value */
616 http_t *http = NULL; /* HTTP connection to server */
617 ipp_t *request = NULL, /* IPP Request */
618 *response = NULL; /* IPP Response */
619 cups_lang_t *language = NULL; /* Default language */
621 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
624 DEBUG(5,("cups_job_delete(%s, %p (%d))\n", sharename, pjob, pjob->sysjob));
627 * Make sure we don't ask for passwords...
630 cupsSetPasswordCB(cups_passwd_cb);
633 * Try to connect to the server...
636 if ((http = cups_connect(frame)) == NULL) {
641 * Build an IPP_CANCEL_JOB request, which requires the following
645 * attributes-natural-language
647 * requesting-user-name
652 request->request.op.operation_id = IPP_CANCEL_JOB;
653 request->request.op.request_id = 1;
655 language = cupsLangDefault();
657 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
658 "attributes-charset", NULL, "utf-8");
660 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
661 "attributes-natural-language", NULL, language->language);
663 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/jobs/%d", pjob->sysjob);
665 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri);
667 if (!push_utf8_talloc(frame, &user, pjob->user, &size)) {
671 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
675 * Do the request and get back a response...
678 if ((response = cupsDoRequest(http, request, "/jobs")) != NULL) {
679 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
680 DEBUG(0,("Unable to cancel job %d - %s\n", pjob->sysjob,
681 ippErrorString(cupsLastError())));
686 DEBUG(0,("Unable to cancel job %d - %s\n", pjob->sysjob,
687 ippErrorString(cupsLastError())));
695 cupsLangFree(language);
706 * 'cups_job_pause()' - Pause a job.
709 static int cups_job_pause(int snum, struct printjob *pjob)
711 TALLOC_CTX *frame = talloc_stackframe();
712 int ret = 1; /* Return value */
713 http_t *http = NULL; /* HTTP connection to server */
714 ipp_t *request = NULL, /* IPP Request */
715 *response = NULL; /* IPP Response */
716 cups_lang_t *language = NULL; /* Default language */
718 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
721 DEBUG(5,("cups_job_pause(%d, %p (%d))\n", snum, pjob, pjob->sysjob));
724 * Make sure we don't ask for passwords...
727 cupsSetPasswordCB(cups_passwd_cb);
730 * Try to connect to the server...
733 if ((http = cups_connect(frame)) == NULL) {
738 * Build an IPP_HOLD_JOB request, which requires the following
742 * attributes-natural-language
744 * requesting-user-name
749 request->request.op.operation_id = IPP_HOLD_JOB;
750 request->request.op.request_id = 1;
752 language = cupsLangDefault();
754 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
755 "attributes-charset", NULL, "utf-8");
757 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
758 "attributes-natural-language", NULL, language->language);
760 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/jobs/%d", pjob->sysjob);
762 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri);
764 if (!push_utf8_talloc(frame, &user, pjob->user, &size)) {
767 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
771 * Do the request and get back a response...
774 if ((response = cupsDoRequest(http, request, "/jobs")) != NULL) {
775 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
776 DEBUG(0,("Unable to hold job %d - %s\n", pjob->sysjob,
777 ippErrorString(cupsLastError())));
782 DEBUG(0,("Unable to hold job %d - %s\n", pjob->sysjob,
783 ippErrorString(cupsLastError())));
791 cupsLangFree(language);
802 * 'cups_job_resume()' - Resume a paused job.
805 static int cups_job_resume(int snum, struct printjob *pjob)
807 TALLOC_CTX *frame = talloc_stackframe();
808 int ret = 1; /* Return value */
809 http_t *http = NULL; /* HTTP connection to server */
810 ipp_t *request = NULL, /* IPP Request */
811 *response = NULL; /* IPP Response */
812 cups_lang_t *language = NULL; /* Default language */
814 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
817 DEBUG(5,("cups_job_resume(%d, %p (%d))\n", snum, pjob, pjob->sysjob));
820 * Make sure we don't ask for passwords...
823 cupsSetPasswordCB(cups_passwd_cb);
826 * Try to connect to the server...
829 if ((http = cups_connect(frame)) == NULL) {
834 * Build an IPP_RELEASE_JOB request, which requires the following
838 * attributes-natural-language
840 * requesting-user-name
845 request->request.op.operation_id = IPP_RELEASE_JOB;
846 request->request.op.request_id = 1;
848 language = cupsLangDefault();
850 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
851 "attributes-charset", NULL, "utf-8");
853 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
854 "attributes-natural-language", NULL, language->language);
856 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/jobs/%d", pjob->sysjob);
858 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri);
860 if (!push_utf8_talloc(frame, &user, pjob->user, &size)) {
863 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
867 * Do the request and get back a response...
870 if ((response = cupsDoRequest(http, request, "/jobs")) != NULL) {
871 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
872 DEBUG(0,("Unable to release job %d - %s\n", pjob->sysjob,
873 ippErrorString(cupsLastError())));
878 DEBUG(0,("Unable to release job %d - %s\n", pjob->sysjob,
879 ippErrorString(cupsLastError())));
887 cupsLangFree(language);
898 * 'cups_job_submit()' - Submit a job for printing.
901 static int cups_job_submit(int snum, struct printjob *pjob)
903 TALLOC_CTX *frame = talloc_stackframe();
904 int ret = 1; /* Return value */
905 http_t *http = NULL; /* HTTP connection to server */
906 ipp_t *request = NULL, /* IPP Request */
907 *response = NULL; /* IPP Response */
908 ipp_attribute_t *attr_job_id = NULL; /* IPP Attribute "job-id" */
909 cups_lang_t *language = NULL; /* Default language */
910 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
911 char *new_jobname = NULL;
913 cups_option_t *options = NULL;
914 char *printername = NULL;
916 char *jobname = NULL;
917 char *cupsoptions = NULL;
918 char *filename = NULL;
920 uint32_t jobid = (uint32_t)-1;
922 DEBUG(5,("cups_job_submit(%d, %p)\n", snum, pjob));
925 * Make sure we don't ask for passwords...
928 cupsSetPasswordCB(cups_passwd_cb);
931 * Try to connect to the server...
934 if ((http = cups_connect(frame)) == NULL) {
939 * Build an IPP_PRINT_JOB request, which requires the following
943 * attributes-natural-language
945 * requesting-user-name
951 request->request.op.operation_id = IPP_PRINT_JOB;
952 request->request.op.request_id = 1;
954 language = cupsLangDefault();
956 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
957 "attributes-charset", NULL, "utf-8");
959 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
960 "attributes-natural-language", NULL, language->language);
962 if (!push_utf8_talloc(frame, &printername, lp_printername(snum),
966 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s",
969 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
970 "printer-uri", NULL, uri);
972 if (!push_utf8_talloc(frame, &user, pjob->user, &size)) {
975 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
978 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
979 "job-originating-host-name", NULL,
980 pjob->clientmachine);
982 /* Get the jobid from the filename. */
983 jobid = print_parse_jobid(pjob->filename);
984 if (jobid == (uint32_t)-1) {
985 DEBUG(0,("cups_job_submit: failed to parse jobid from name %s\n",
990 if (!push_utf8_talloc(frame, &jobname, pjob->jobname, &size)) {
993 new_jobname = talloc_asprintf(frame,
994 "%s%.8u %s", PRINT_SPOOL_PREFIX,
997 if (new_jobname == NULL) {
1001 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name", NULL,
1005 * add any options defined in smb.conf
1008 if (!push_utf8_talloc(frame, &cupsoptions, lp_cups_options(snum), &size)) {
1013 num_options = cupsParseOptions(cupsoptions, num_options, &options);
1016 cupsEncodeOptions(request, num_options, options);
1019 * Do the request and get back a response...
1022 slprintf(uri, sizeof(uri) - 1, "/printers/%s", printername);
1024 if (!push_utf8_talloc(frame, &filename, pjob->filename, &size)) {
1027 if ((response = cupsDoFileRequest(http, request, uri, pjob->filename)) != NULL) {
1028 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1029 DEBUG(0,("Unable to print file to %s - %s\n",
1030 lp_printername(snum),
1031 ippErrorString(cupsLastError())));
1034 attr_job_id = ippFindAttribute(response, "job-id", IPP_TAG_INTEGER);
1036 pjob->sysjob = attr_job_id->values[0].integer;
1037 DEBUG(5,("cups_job_submit: job-id %d\n", pjob->sysjob));
1039 DEBUG(0,("Missing job-id attribute in IPP response"));
1043 DEBUG(0,("Unable to print file to `%s' - %s\n",
1044 lp_printername(snum),
1045 ippErrorString(cupsLastError())));
1049 unlink(pjob->filename);
1050 /* else print_job_end will do it for us */
1054 ippDelete(response);
1057 cupsLangFree(language);
1068 * 'cups_queue_get()' - Get all the jobs in the print queue.
1071 static int cups_queue_get(const char *sharename,
1072 enum printing_types printing_type,
1074 print_queue_struct **q,
1075 print_status_struct *status)
1077 TALLOC_CTX *frame = talloc_stackframe();
1078 char *printername = NULL;
1079 http_t *http = NULL; /* HTTP connection to server */
1080 ipp_t *request = NULL, /* IPP Request */
1081 *response = NULL; /* IPP Response */
1082 ipp_attribute_t *attr = NULL; /* Current attribute */
1083 cups_lang_t *language = NULL; /* Default language */
1084 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
1085 int qcount = 0, /* Number of active queue entries */
1086 qalloc = 0; /* Number of queue entries allocated */
1087 print_queue_struct *queue = NULL, /* Queue entries */
1088 *temp; /* Temporary pointer for queue */
1089 char *user_name = NULL, /* job-originating-user-name attribute */
1090 *job_name = NULL; /* job-name attribute */
1091 int job_id; /* job-id attribute */
1092 int job_k_octets; /* job-k-octets attribute */
1093 time_t job_time; /* time-at-creation attribute */
1094 ipp_jstate_t job_status; /* job-status attribute */
1095 int job_priority; /* job-priority attribute */
1097 static const char *jattrs[] = /* Requested job attributes */
1102 "job-originating-user-name",
1107 static const char *pattrs[] = /* Requested printer attributes */
1110 "printer-state-message"
1115 /* HACK ALERT!!! The problem with support the 'printer name'
1116 option is that we key the tdb off the sharename. So we will
1117 overload the lpq_command string to pass in the printername
1118 (which is basically what we do for non-cups printers ... using
1119 the lpq_command to get the queue listing). */
1121 if (!push_utf8_talloc(frame, &printername, lpq_command, &size)) {
1124 DEBUG(5,("cups_queue_get(%s, %p, %p)\n", lpq_command, q, status));
1127 * Make sure we don't ask for passwords...
1130 cupsSetPasswordCB(cups_passwd_cb);
1133 * Try to connect to the server...
1136 if ((http = cups_connect(frame)) == NULL) {
1141 * Generate the printer URI...
1144 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s", printername);
1147 * Build an IPP_GET_JOBS request, which requires the following
1150 * attributes-charset
1151 * attributes-natural-language
1152 * requested-attributes
1158 request->request.op.operation_id = IPP_GET_JOBS;
1159 request->request.op.request_id = 1;
1161 language = cupsLangDefault();
1163 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1164 "attributes-charset", NULL, "utf-8");
1166 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1167 "attributes-natural-language", NULL, language->language);
1169 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1170 "requested-attributes",
1171 (sizeof(jattrs) / sizeof(jattrs[0])),
1174 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1175 "printer-uri", NULL, uri);
1178 * Do the request and get back a response...
1181 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
1182 DEBUG(0,("Unable to get jobs for %s - %s\n", uri,
1183 ippErrorString(cupsLastError())));
1187 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1188 DEBUG(0,("Unable to get jobs for %s - %s\n", uri,
1189 ippErrorString(response->request.status.status_code)));
1194 * Process the jobs...
1201 for (attr = response->attrs; attr != NULL; attr = attr->next) {
1203 * Skip leading attributes until we hit a job...
1206 while (attr != NULL && attr->group_tag != IPP_TAG_JOB)
1213 * Allocate memory as needed...
1215 if (qcount >= qalloc) {
1218 queue = SMB_REALLOC_ARRAY(queue, print_queue_struct, qalloc);
1220 if (queue == NULL) {
1221 DEBUG(0,("cups_queue_get: Not enough memory!"));
1227 temp = queue + qcount;
1228 memset(temp, 0, sizeof(print_queue_struct));
1231 * Pull the needed attributes from this job...
1236 job_status = IPP_JOB_PENDING;
1242 while (attr != NULL && attr->group_tag == IPP_TAG_JOB) {
1243 if (attr->name == NULL) {
1248 if (strcmp(attr->name, "job-id") == 0 &&
1249 attr->value_tag == IPP_TAG_INTEGER)
1250 job_id = attr->values[0].integer;
1252 if (strcmp(attr->name, "job-k-octets") == 0 &&
1253 attr->value_tag == IPP_TAG_INTEGER)
1254 job_k_octets = attr->values[0].integer;
1256 if (strcmp(attr->name, "job-priority") == 0 &&
1257 attr->value_tag == IPP_TAG_INTEGER)
1258 job_priority = attr->values[0].integer;
1260 if (strcmp(attr->name, "job-state") == 0 &&
1261 attr->value_tag == IPP_TAG_ENUM)
1262 job_status = (ipp_jstate_t)(attr->values[0].integer);
1264 if (strcmp(attr->name, "time-at-creation") == 0 &&
1265 attr->value_tag == IPP_TAG_INTEGER)
1266 job_time = attr->values[0].integer;
1268 if (strcmp(attr->name, "job-name") == 0 &&
1269 attr->value_tag == IPP_TAG_NAME) {
1270 if (!pull_utf8_talloc(frame,
1272 attr->values[0].string.text,
1278 if (strcmp(attr->name, "job-originating-user-name") == 0 &&
1279 attr->value_tag == IPP_TAG_NAME) {
1280 if (!pull_utf8_talloc(frame,
1282 attr->values[0].string.text,
1292 * See if we have everything needed...
1295 if (user_name == NULL || job_name == NULL || job_id == 0) {
1303 temp->size = job_k_octets * 1024;
1304 temp->status = job_status == IPP_JOB_PENDING ? LPQ_QUEUED :
1305 job_status == IPP_JOB_STOPPED ? LPQ_PAUSED :
1306 job_status == IPP_JOB_HELD ? LPQ_PAUSED :
1308 temp->priority = job_priority;
1309 temp->time = job_time;
1310 strlcpy(temp->fs_user, user_name, sizeof(temp->fs_user));
1311 strlcpy(temp->fs_file, job_name, sizeof(temp->fs_file));
1319 ippDelete(response);
1323 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the
1324 * following attributes:
1326 * attributes-charset
1327 * attributes-natural-language
1328 * requested-attributes
1334 request->request.op.operation_id = IPP_GET_PRINTER_ATTRIBUTES;
1335 request->request.op.request_id = 1;
1337 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1338 "attributes-charset", NULL, "utf-8");
1340 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1341 "attributes-natural-language", NULL, language->language);
1343 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1344 "requested-attributes",
1345 (sizeof(pattrs) / sizeof(pattrs[0])),
1348 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1349 "printer-uri", NULL, uri);
1352 * Do the request and get back a response...
1355 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
1356 DEBUG(0,("Unable to get printer status for %s - %s\n", printername,
1357 ippErrorString(cupsLastError())));
1361 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1362 DEBUG(0,("Unable to get printer status for %s - %s\n", printername,
1363 ippErrorString(response->request.status.status_code)));
1368 * Get the current printer status and convert it to the SAMBA values.
1371 if ((attr = ippFindAttribute(response, "printer-state", IPP_TAG_ENUM)) != NULL) {
1372 if (attr->values[0].integer == IPP_PRINTER_STOPPED)
1373 status->status = LPSTAT_STOPPED;
1375 status->status = LPSTAT_OK;
1378 if ((attr = ippFindAttribute(response, "printer-state-message",
1379 IPP_TAG_TEXT)) != NULL) {
1381 if (!pull_utf8_talloc(frame, &msg,
1382 attr->values[0].string.text,
1388 fstrcpy(status->message, msg);
1394 * Return the job queue...
1400 ippDelete(response);
1403 cupsLangFree(language);
1414 * 'cups_queue_pause()' - Pause a print queue.
1417 static int cups_queue_pause(int snum)
1419 TALLOC_CTX *frame = talloc_stackframe();
1420 int ret = 1; /* Return value */
1421 http_t *http = NULL; /* HTTP connection to server */
1422 ipp_t *request = NULL, /* IPP Request */
1423 *response = NULL; /* IPP Response */
1424 cups_lang_t *language = NULL; /* Default language */
1425 char *printername = NULL;
1426 char *username = NULL;
1427 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
1430 DEBUG(5,("cups_queue_pause(%d)\n", snum));
1433 * Make sure we don't ask for passwords...
1436 cupsSetPasswordCB(cups_passwd_cb);
1439 * Try to connect to the server...
1442 if ((http = cups_connect(frame)) == NULL) {
1447 * Build an IPP_PAUSE_PRINTER request, which requires the following
1450 * attributes-charset
1451 * attributes-natural-language
1453 * requesting-user-name
1458 request->request.op.operation_id = IPP_PAUSE_PRINTER;
1459 request->request.op.request_id = 1;
1461 language = cupsLangDefault();
1463 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1464 "attributes-charset", NULL, "utf-8");
1466 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1467 "attributes-natural-language", NULL, language->language);
1469 if (!push_utf8_talloc(frame, &printername, lp_printername(snum),
1473 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s",
1476 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
1478 if (!push_utf8_talloc(frame, &username, current_user_info.unix_name, &size)) {
1481 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
1485 * Do the request and get back a response...
1488 if ((response = cupsDoRequest(http, request, "/admin/")) != NULL) {
1489 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1490 DEBUG(0,("Unable to pause printer %s - %s\n",
1491 lp_printername(snum),
1492 ippErrorString(cupsLastError())));
1497 DEBUG(0,("Unable to pause printer %s - %s\n",
1498 lp_printername(snum),
1499 ippErrorString(cupsLastError())));
1504 ippDelete(response);
1507 cupsLangFree(language);
1518 * 'cups_queue_resume()' - Restart a print queue.
1521 static int cups_queue_resume(int snum)
1523 TALLOC_CTX *frame = talloc_stackframe();
1524 int ret = 1; /* Return value */
1525 http_t *http = NULL; /* HTTP connection to server */
1526 ipp_t *request = NULL, /* IPP Request */
1527 *response = NULL; /* IPP Response */
1528 cups_lang_t *language = NULL; /* Default language */
1529 char *printername = NULL;
1530 char *username = NULL;
1531 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
1534 DEBUG(5,("cups_queue_resume(%d)\n", snum));
1537 * Make sure we don't ask for passwords...
1540 cupsSetPasswordCB(cups_passwd_cb);
1543 * Try to connect to the server...
1546 if ((http = cups_connect(frame)) == NULL) {
1551 * Build an IPP_RESUME_PRINTER request, which requires the following
1554 * attributes-charset
1555 * attributes-natural-language
1557 * requesting-user-name
1562 request->request.op.operation_id = IPP_RESUME_PRINTER;
1563 request->request.op.request_id = 1;
1565 language = cupsLangDefault();
1567 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1568 "attributes-charset", NULL, "utf-8");
1570 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1571 "attributes-natural-language", NULL, language->language);
1573 if (!push_utf8_talloc(frame, &printername, lp_printername(snum),
1577 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s",
1580 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
1582 if (!push_utf8_talloc(frame, &username, current_user_info.unix_name, &size)) {
1585 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
1589 * Do the request and get back a response...
1592 if ((response = cupsDoRequest(http, request, "/admin/")) != NULL) {
1593 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1594 DEBUG(0,("Unable to resume printer %s - %s\n",
1595 lp_printername(snum),
1596 ippErrorString(cupsLastError())));
1601 DEBUG(0,("Unable to resume printer %s - %s\n",
1602 lp_printername(snum),
1603 ippErrorString(cupsLastError())));
1608 ippDelete(response);
1611 cupsLangFree(language);
1620 /*******************************************************************
1621 * CUPS printing interface definitions...
1622 ******************************************************************/
1624 struct printif cups_printif =
1636 bool cups_pull_comment_location(TALLOC_CTX *mem_ctx,
1637 const char *printername,
1641 TALLOC_CTX *frame = talloc_stackframe();
1642 http_t *http = NULL; /* HTTP connection to server */
1643 ipp_t *request = NULL, /* IPP Request */
1644 *response = NULL; /* IPP Response */
1645 ipp_attribute_t *attr; /* Current attribute */
1646 cups_lang_t *language = NULL; /* Default language */
1647 char uri[HTTP_MAX_URI];
1648 char *server = NULL;
1649 char *sharename = NULL;
1651 static const char *requested[] =/* Requested attributes */
1660 DEBUG(5, ("pulling %s location\n", printername));
1663 * Make sure we don't ask for passwords...
1666 cupsSetPasswordCB(cups_passwd_cb);
1669 * Try to connect to the server...
1672 if ((http = cups_connect(frame)) == NULL) {
1678 request->request.op.operation_id = IPP_GET_PRINTER_ATTRIBUTES;
1679 request->request.op.request_id = 1;
1681 language = cupsLangDefault();
1683 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1684 "attributes-charset", NULL, "utf-8");
1686 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1687 "attributes-natural-language", NULL, language->language);
1689 if (lp_cups_server() != NULL && strlen(lp_cups_server()) > 0) {
1690 if (!push_utf8_talloc(frame, &server, lp_cups_server(), &size)) {
1694 server = talloc_strdup(frame,cupsServer());
1699 if (!push_utf8_talloc(frame, &sharename, printername, &size)) {
1702 slprintf(uri, sizeof(uri) - 1, "ipp://%s/printers/%s",
1705 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1706 "printer-uri", NULL, uri);
1708 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1709 "requested-attributes",
1710 (sizeof(requested) / sizeof(requested[0])),
1714 * Do the request and get back a response...
1717 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
1718 DEBUG(0,("Unable to get printer attributes - %s\n",
1719 ippErrorString(cupsLastError())));
1723 for (attr = response->attrs; attr != NULL;) {
1725 * Skip leading attributes until we hit a printer...
1728 while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)
1735 * Pull the needed attributes from this printer...
1738 while ( attr && (attr->group_tag == IPP_TAG_PRINTER) ) {
1739 if (strcmp(attr->name, "printer-name") == 0 &&
1740 attr->value_tag == IPP_TAG_NAME) {
1741 if (!pull_utf8_talloc(frame,
1743 attr->values[0].string.text,
1749 /* Grab the comment if we don't have one */
1750 if ( (strcmp(attr->name, "printer-info") == 0)
1751 && (attr->value_tag == IPP_TAG_TEXT))
1753 if (!pull_utf8_talloc(mem_ctx,
1755 attr->values[0].string.text,
1759 DEBUG(5,("cups_pull_comment_location: Using cups comment: %s\n",
1763 /* Grab the location if we don't have one */
1764 if ( (strcmp(attr->name, "printer-location") == 0)
1765 && (attr->value_tag == IPP_TAG_TEXT))
1767 if (!pull_utf8_talloc(mem_ctx,
1769 attr->values[0].string.text,
1773 DEBUG(5,("cups_pull_comment_location: Using cups location: %s\n",
1781 * We have everything needed...
1792 ippDelete(response);
1799 cupsLangFree(language);
1809 /* this keeps fussy compilers happy */
1810 void print_cups_dummy(void);
1811 void print_cups_dummy(void) {}
1812 #endif /* HAVE_CUPS */