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"
30 #include <cups/cups.h>
31 #include <cups/language.h>
33 static SIG_ATOMIC_T gotalarm;
35 /***************************************************************
36 Signal function to tell us we timed out.
37 ****************************************************************/
39 static void gotalarm_sig(int signum)
44 extern userdom_struct current_user_info;
47 * 'cups_passwd_cb()' - The CUPS password callback...
50 static const char * /* O - Password or NULL */
51 cups_passwd_cb(const char *prompt) /* I - Prompt */
54 * Always return NULL to indicate that no password is available...
60 static http_t *cups_connect(TALLOC_CTX *frame)
63 char *server = NULL, *p = NULL;
65 int timeout = lp_cups_connection_timeout();
68 if (lp_cups_server() != NULL && strlen(lp_cups_server()) > 0) {
69 if (!push_utf8_talloc(frame, &server, lp_cups_server(), &size)) {
73 server = talloc_strdup(frame,cupsServer());
79 p = strchr(server, ':');
87 DEBUG(10, ("connecting to cups server %s:%d\n",
93 CatchSignal(SIGALRM, gotalarm_sig);
97 #ifdef HAVE_HTTPCONNECTENCRYPT
98 http = httpConnectEncrypt(server, port, lp_cups_encrypt());
100 http = httpConnect(server, port);
104 CatchSignal(SIGALRM, SIG_IGN);
108 DEBUG(0,("Unable to connect to CUPS server %s:%d - %s\n",
109 server, port, strerror(errno)));
115 static void send_pcap_info(const char *name, const char *info, void *pd)
118 size_t namelen = name ? strlen(name)+1 : 0;
119 size_t infolen = info ? strlen(info)+1 : 0;
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",
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",
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",
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",
151 static bool cups_cache_reload_async(int fd)
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 */
170 DEBUG(5, ("reloading cups printcap cache\n"));
173 * Make sure we don't ask for passwords...
176 cupsSetPasswordCB(cups_passwd_cb);
179 * Try to connect to the server...
182 if ((http = cups_connect(frame)) == NULL) {
187 * Build a CUPS_GET_PRINTERS request, which requires the following
191 * attributes-natural-language
192 * requested-attributes
197 request->request.op.operation_id = CUPS_GET_PRINTERS;
198 request->request.op.request_id = 1;
200 language = cupsLangDefault();
202 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
203 "attributes-charset", NULL, "utf-8");
205 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
206 "attributes-natural-language", NULL, language->language);
208 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
209 "requested-attributes",
210 (sizeof(requested) / sizeof(requested[0])),
214 * Do the request and get back a response...
217 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
218 DEBUG(0,("Unable to get printer list - %s\n",
219 ippErrorString(cupsLastError())));
223 for (attr = response->attrs; attr != NULL;) {
225 * Skip leading attributes until we hit a printer...
228 while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)
235 * Pull the needed attributes from this printer...
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,
246 attr->values[0].string.text,
252 if (strcmp(attr->name, "printer-info") == 0 &&
253 attr->value_tag == IPP_TAG_TEXT) {
254 if (!pull_utf8_talloc(frame,
256 attr->values[0].string.text,
266 * See if we have everything needed...
272 if (!pcap_cache_add_specific(&tmp_pcap_cache, name, info)) {
281 * Build a CUPS_GET_CLASSES request, which requires the following
285 * attributes-natural-language
286 * requested-attributes
291 request->request.op.operation_id = CUPS_GET_CLASSES;
292 request->request.op.request_id = 1;
294 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
295 "attributes-charset", NULL, "utf-8");
297 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
298 "attributes-natural-language", NULL, language->language);
300 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
301 "requested-attributes",
302 (sizeof(requested) / sizeof(requested[0])),
306 * Do the request and get back a response...
309 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
310 DEBUG(0,("Unable to get printer list - %s\n",
311 ippErrorString(cupsLastError())));
315 for (attr = response->attrs; attr != NULL;) {
317 * Skip leading attributes until we hit a printer...
320 while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)
327 * Pull the needed attributes from this printer...
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,
338 attr->values[0].string.text,
344 if (strcmp(attr->name, "printer-info") == 0 &&
345 attr->value_tag == IPP_TAG_TEXT) {
346 if (!pull_utf8_talloc(frame,
348 attr->values[0].string.text,
358 * See if we have everything needed...
364 if (!pcap_cache_add_specific(&tmp_pcap_cache, name, info)) {
376 cupsLangFree(language);
381 /* Send all the entries up the pipe. */
382 if (tmp_pcap_cache) {
383 pcap_printer_fn_specific(tmp_pcap_cache,
387 pcap_cache_destroy_specific(&tmp_pcap_cache);
393 static struct pcap_cache *local_pcap_copy;
394 struct fd_event *cache_fd_event;
396 static bool cups_pcap_load_async(int *pfd)
403 if (cache_fd_event) {
404 DEBUG(3,("cups_pcap_load_async: already waiting for "
405 "a refresh event\n" ));
409 DEBUG(5,("cups_pcap_load_async: asynchronously loading cups printers\n"));
411 if (pipe(fds) == -1) {
416 if (pid == (pid_t)-1) {
417 DEBUG(10,("cups_pcap_load_async: fork failed %s\n",
425 DEBUG(10,("cups_pcap_load_async: child pid = %u\n",
426 (unsigned int)pid ));
435 close_all_print_db();
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");
444 cups_cache_reload_async(fds[1]);
449 static void cups_async_callback(struct event_context *event_ctx,
450 struct fd_event *event,
454 TALLOC_CTX *frame = talloc_stackframe();
456 struct pcap_cache *tmp_pcap_cache = NULL;
458 DEBUG(5,("cups_async_callback: callback received for printer data. "
462 char *name = NULL, *info = NULL;
463 size_t namelen = 0, infolen = 0;
466 ret = sys_read(fd, &namelen, sizeof(namelen));
471 if (ret != sizeof(namelen)) {
472 DEBUG(10,("cups_async_callback: namelen read failed %d %s\n",
473 errno, strerror(errno)));
477 DEBUG(11,("cups_async_callback: read namelen %u\n",
478 (unsigned int)namelen));
480 ret = sys_read(fd, &infolen, sizeof(infolen));
485 if (ret != sizeof(infolen)) {
486 DEBUG(10,("cups_async_callback: infolen read failed %s\n",
491 DEBUG(11,("cups_async_callback: read infolen %u\n",
492 (unsigned int)infolen));
495 name = TALLOC_ARRAY(frame, char, namelen);
499 ret = sys_read(fd, name, namelen);
504 if (ret != namelen) {
505 DEBUG(10,("cups_async_callback: name read failed %s\n",
509 DEBUG(11,("cups_async_callback: read name %s\n",
515 info = TALLOC_ARRAY(frame, char, infolen);
519 ret = sys_read(fd, info, infolen);
524 if (ret != infolen) {
525 DEBUG(10,("cups_async_callback: info read failed %s\n",
529 DEBUG(11,("cups_async_callback: read info %s\n",
535 /* Add to our local pcap cache. */
536 pcap_cache_add_specific(&tmp_pcap_cache, name, info);
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;
547 /* And the systemwide pcap cache. */
548 pcap_cache_replace(local_pcap_copy);
550 DEBUG(2,("cups_async_callback: failed to read a new "
555 TALLOC_FREE(cache_fd_event);
558 bool cups_cache_reload(void)
560 int *p_pipe_fd = TALLOC_P(NULL, int);
568 /* Set up an async refresh. */
569 if (!cups_pcap_load_async(p_pipe_fd)) {
572 if (!local_pcap_copy) {
573 /* We have no local cache, wait directly for
574 * async refresh to complete.
576 DEBUG(10,("cups_cache_reload: sync read on fd %d\n",
579 cups_async_callback(server_event_context(),
583 if (!local_pcap_copy) {
587 /* Replace the system cache with our
589 pcap_cache_replace(local_pcap_copy);
591 DEBUG(10,("cups_cache_reload: async read on fd %d\n",
594 /* Trigger an event when the pipe can be read. */
595 cache_fd_event = event_add_fd(server_event_context(),
600 if (!cache_fd_event) {
602 TALLOC_FREE(p_pipe_fd);
610 * 'cups_job_delete()' - Delete a job.
613 static int cups_job_delete(const char *sharename, const char *lprm_command, struct printjob *pjob)
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 */
622 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
625 DEBUG(5,("cups_job_delete(%s, %p (%d))\n", sharename, pjob, pjob->sysjob));
628 * Make sure we don't ask for passwords...
631 cupsSetPasswordCB(cups_passwd_cb);
634 * Try to connect to the server...
637 if ((http = cups_connect(frame)) == NULL) {
642 * Build an IPP_CANCEL_JOB request, which requires the following
646 * attributes-natural-language
648 * requesting-user-name
653 request->request.op.operation_id = IPP_CANCEL_JOB;
654 request->request.op.request_id = 1;
656 language = cupsLangDefault();
658 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
659 "attributes-charset", NULL, "utf-8");
661 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
662 "attributes-natural-language", NULL, language->language);
664 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/jobs/%d", pjob->sysjob);
666 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri);
668 if (!push_utf8_talloc(frame, &user, pjob->user, &size)) {
672 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
676 * Do the request and get back a response...
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())));
687 DEBUG(0,("Unable to cancel job %d - %s\n", pjob->sysjob,
688 ippErrorString(cupsLastError())));
696 cupsLangFree(language);
707 * 'cups_job_pause()' - Pause a job.
710 static int cups_job_pause(int snum, struct printjob *pjob)
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 */
719 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
722 DEBUG(5,("cups_job_pause(%d, %p (%d))\n", snum, pjob, pjob->sysjob));
725 * Make sure we don't ask for passwords...
728 cupsSetPasswordCB(cups_passwd_cb);
731 * Try to connect to the server...
734 if ((http = cups_connect(frame)) == NULL) {
739 * Build an IPP_HOLD_JOB request, which requires the following
743 * attributes-natural-language
745 * requesting-user-name
750 request->request.op.operation_id = IPP_HOLD_JOB;
751 request->request.op.request_id = 1;
753 language = cupsLangDefault();
755 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
756 "attributes-charset", NULL, "utf-8");
758 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
759 "attributes-natural-language", NULL, language->language);
761 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/jobs/%d", pjob->sysjob);
763 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri);
765 if (!push_utf8_talloc(frame, &user, pjob->user, &size)) {
768 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
772 * Do the request and get back a response...
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())));
783 DEBUG(0,("Unable to hold job %d - %s\n", pjob->sysjob,
784 ippErrorString(cupsLastError())));
792 cupsLangFree(language);
803 * 'cups_job_resume()' - Resume a paused job.
806 static int cups_job_resume(int snum, struct printjob *pjob)
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 */
815 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
818 DEBUG(5,("cups_job_resume(%d, %p (%d))\n", snum, pjob, pjob->sysjob));
821 * Make sure we don't ask for passwords...
824 cupsSetPasswordCB(cups_passwd_cb);
827 * Try to connect to the server...
830 if ((http = cups_connect(frame)) == NULL) {
835 * Build an IPP_RELEASE_JOB request, which requires the following
839 * attributes-natural-language
841 * requesting-user-name
846 request->request.op.operation_id = IPP_RELEASE_JOB;
847 request->request.op.request_id = 1;
849 language = cupsLangDefault();
851 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
852 "attributes-charset", NULL, "utf-8");
854 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
855 "attributes-natural-language", NULL, language->language);
857 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/jobs/%d", pjob->sysjob);
859 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri);
861 if (!push_utf8_talloc(frame, &user, pjob->user, &size)) {
864 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
868 * Do the request and get back a response...
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())));
879 DEBUG(0,("Unable to release job %d - %s\n", pjob->sysjob,
880 ippErrorString(cupsLastError())));
888 cupsLangFree(language);
899 * 'cups_job_submit()' - Submit a job for printing.
902 static int cups_job_submit(int snum, struct printjob *pjob)
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;
915 cups_option_t *options = NULL;
916 char *printername = NULL;
918 char *jobname = NULL;
919 char *cupsoptions = NULL;
920 char *filename = NULL;
922 uint32_t jobid = (uint32_t)-1;
923 char addr[INET6_ADDRSTRLEN];
925 DEBUG(5,("cups_job_submit(%d, %p)\n", snum, pjob));
928 * Make sure we don't ask for passwords...
931 cupsSetPasswordCB(cups_passwd_cb);
934 * Try to connect to the server...
937 if ((http = cups_connect(frame)) == NULL) {
942 * Build an IPP_PRINT_JOB request, which requires the following
946 * attributes-natural-language
948 * requesting-user-name
954 request->request.op.operation_id = IPP_PRINT_JOB;
955 request->request.op.request_id = 1;
957 language = cupsLangDefault();
959 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
960 "attributes-charset", NULL, "utf-8");
962 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
963 "attributes-natural-language", NULL, language->language);
965 if (!push_utf8_talloc(frame, &printername, lp_printername(snum),
969 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s",
972 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
973 "printer-uri", NULL, uri);
975 if (!push_utf8_talloc(frame, &user, pjob->user, &size)) {
978 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
981 clientname = client_name(get_client_fd());
982 if (strcmp(clientname, "UNKNOWN") == 0) {
983 clientname = client_addr(get_client_fd(),addr,sizeof(addr));
986 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
987 "job-originating-host-name", NULL,
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",
998 if (!push_utf8_talloc(frame, &jobname, pjob->jobname, &size)) {
1001 new_jobname = talloc_asprintf(frame,
1002 "%s%.8u %s", PRINT_SPOOL_PREFIX,
1003 (unsigned int)jobid,
1005 if (new_jobname == NULL) {
1009 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name", NULL,
1013 * add any options defined in smb.conf
1016 if (!push_utf8_talloc(frame, &cupsoptions, lp_cups_options(snum), &size)) {
1021 num_options = cupsParseOptions(cupsoptions, num_options, &options);
1024 cupsEncodeOptions(request, num_options, options);
1027 * Do the request and get back a response...
1030 slprintf(uri, sizeof(uri) - 1, "/printers/%s", printername);
1032 if (!push_utf8_talloc(frame, &filename, pjob->filename, &size)) {
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())));
1042 attr_job_id = ippFindAttribute(response, "job-id", IPP_TAG_INTEGER);
1044 pjob->sysjob = attr_job_id->values[0].integer;
1045 DEBUG(5,("cups_job_submit: job-id %d\n", pjob->sysjob));
1047 DEBUG(0,("Missing job-id attribute in IPP response"));
1051 DEBUG(0,("Unable to print file to `%s' - %s\n",
1052 lp_printername(snum),
1053 ippErrorString(cupsLastError())));
1057 unlink(pjob->filename);
1058 /* else print_job_end will do it for us */
1062 ippDelete(response);
1065 cupsLangFree(language);
1076 * 'cups_queue_get()' - Get all the jobs in the print queue.
1079 static int cups_queue_get(const char *sharename,
1080 enum printing_types printing_type,
1082 print_queue_struct **q,
1083 print_status_struct *status)
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 */
1105 static const char *jattrs[] = /* Requested job attributes */
1110 "job-originating-user-name",
1115 static const char *pattrs[] = /* Requested printer attributes */
1118 "printer-state-message"
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). */
1129 if (!push_utf8_talloc(frame, &printername, lpq_command, &size)) {
1132 DEBUG(5,("cups_queue_get(%s, %p, %p)\n", lpq_command, q, status));
1135 * Make sure we don't ask for passwords...
1138 cupsSetPasswordCB(cups_passwd_cb);
1141 * Try to connect to the server...
1144 if ((http = cups_connect(frame)) == NULL) {
1149 * Generate the printer URI...
1152 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s", printername);
1155 * Build an IPP_GET_JOBS request, which requires the following
1158 * attributes-charset
1159 * attributes-natural-language
1160 * requested-attributes
1166 request->request.op.operation_id = IPP_GET_JOBS;
1167 request->request.op.request_id = 1;
1169 language = cupsLangDefault();
1171 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1172 "attributes-charset", NULL, "utf-8");
1174 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1175 "attributes-natural-language", NULL, language->language);
1177 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1178 "requested-attributes",
1179 (sizeof(jattrs) / sizeof(jattrs[0])),
1182 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1183 "printer-uri", NULL, uri);
1186 * Do the request and get back a response...
1189 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
1190 DEBUG(0,("Unable to get jobs for %s - %s\n", uri,
1191 ippErrorString(cupsLastError())));
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)));
1202 * Process the jobs...
1209 for (attr = response->attrs; attr != NULL; attr = attr->next) {
1211 * Skip leading attributes until we hit a job...
1214 while (attr != NULL && attr->group_tag != IPP_TAG_JOB)
1221 * Allocate memory as needed...
1223 if (qcount >= qalloc) {
1226 queue = SMB_REALLOC_ARRAY(queue, print_queue_struct, qalloc);
1228 if (queue == NULL) {
1229 DEBUG(0,("cups_queue_get: Not enough memory!"));
1235 temp = queue + qcount;
1236 memset(temp, 0, sizeof(print_queue_struct));
1239 * Pull the needed attributes from this job...
1244 job_status = IPP_JOB_PENDING;
1250 while (attr != NULL && attr->group_tag == IPP_TAG_JOB) {
1251 if (attr->name == NULL) {
1256 if (strcmp(attr->name, "job-id") == 0 &&
1257 attr->value_tag == IPP_TAG_INTEGER)
1258 job_id = attr->values[0].integer;
1260 if (strcmp(attr->name, "job-k-octets") == 0 &&
1261 attr->value_tag == IPP_TAG_INTEGER)
1262 job_k_octets = attr->values[0].integer;
1264 if (strcmp(attr->name, "job-priority") == 0 &&
1265 attr->value_tag == IPP_TAG_INTEGER)
1266 job_priority = attr->values[0].integer;
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);
1272 if (strcmp(attr->name, "time-at-creation") == 0 &&
1273 attr->value_tag == IPP_TAG_INTEGER)
1274 job_time = attr->values[0].integer;
1276 if (strcmp(attr->name, "job-name") == 0 &&
1277 attr->value_tag == IPP_TAG_NAME) {
1278 if (!pull_utf8_talloc(frame,
1280 attr->values[0].string.text,
1286 if (strcmp(attr->name, "job-originating-user-name") == 0 &&
1287 attr->value_tag == IPP_TAG_NAME) {
1288 if (!pull_utf8_talloc(frame,
1290 attr->values[0].string.text,
1300 * See if we have everything needed...
1303 if (user_name == NULL || job_name == NULL || job_id == 0) {
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 :
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));
1327 ippDelete(response);
1331 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the
1332 * following attributes:
1334 * attributes-charset
1335 * attributes-natural-language
1336 * requested-attributes
1342 request->request.op.operation_id = IPP_GET_PRINTER_ATTRIBUTES;
1343 request->request.op.request_id = 1;
1345 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1346 "attributes-charset", NULL, "utf-8");
1348 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1349 "attributes-natural-language", NULL, language->language);
1351 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1352 "requested-attributes",
1353 (sizeof(pattrs) / sizeof(pattrs[0])),
1356 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1357 "printer-uri", NULL, uri);
1360 * Do the request and get back a response...
1363 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
1364 DEBUG(0,("Unable to get printer status for %s - %s\n", printername,
1365 ippErrorString(cupsLastError())));
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)));
1376 * Get the current printer status and convert it to the SAMBA values.
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;
1383 status->status = LPSTAT_OK;
1386 if ((attr = ippFindAttribute(response, "printer-state-message",
1387 IPP_TAG_TEXT)) != NULL) {
1389 if (!pull_utf8_talloc(frame, &msg,
1390 attr->values[0].string.text,
1396 fstrcpy(status->message, msg);
1402 * Return the job queue...
1408 ippDelete(response);
1411 cupsLangFree(language);
1422 * 'cups_queue_pause()' - Pause a print queue.
1425 static int cups_queue_pause(int snum)
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 */
1438 DEBUG(5,("cups_queue_pause(%d)\n", snum));
1441 * Make sure we don't ask for passwords...
1444 cupsSetPasswordCB(cups_passwd_cb);
1447 * Try to connect to the server...
1450 if ((http = cups_connect(frame)) == NULL) {
1455 * Build an IPP_PAUSE_PRINTER request, which requires the following
1458 * attributes-charset
1459 * attributes-natural-language
1461 * requesting-user-name
1466 request->request.op.operation_id = IPP_PAUSE_PRINTER;
1467 request->request.op.request_id = 1;
1469 language = cupsLangDefault();
1471 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1472 "attributes-charset", NULL, "utf-8");
1474 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1475 "attributes-natural-language", NULL, language->language);
1477 if (!push_utf8_talloc(frame, &printername, lp_printername(snum),
1481 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s",
1484 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
1486 if (!push_utf8_talloc(frame, &username, current_user_info.unix_name, &size)) {
1489 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
1493 * Do the request and get back a response...
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())));
1505 DEBUG(0,("Unable to pause printer %s - %s\n",
1506 lp_printername(snum),
1507 ippErrorString(cupsLastError())));
1512 ippDelete(response);
1515 cupsLangFree(language);
1526 * 'cups_queue_resume()' - Restart a print queue.
1529 static int cups_queue_resume(int snum)
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 */
1542 DEBUG(5,("cups_queue_resume(%d)\n", snum));
1545 * Make sure we don't ask for passwords...
1548 cupsSetPasswordCB(cups_passwd_cb);
1551 * Try to connect to the server...
1554 if ((http = cups_connect(frame)) == NULL) {
1559 * Build an IPP_RESUME_PRINTER request, which requires the following
1562 * attributes-charset
1563 * attributes-natural-language
1565 * requesting-user-name
1570 request->request.op.operation_id = IPP_RESUME_PRINTER;
1571 request->request.op.request_id = 1;
1573 language = cupsLangDefault();
1575 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1576 "attributes-charset", NULL, "utf-8");
1578 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1579 "attributes-natural-language", NULL, language->language);
1581 if (!push_utf8_talloc(frame, &printername, lp_printername(snum),
1585 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s",
1588 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
1590 if (!push_utf8_talloc(frame, &username, current_user_info.unix_name, &size)) {
1593 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
1597 * Do the request and get back a response...
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())));
1609 DEBUG(0,("Unable to resume printer %s - %s\n",
1610 lp_printername(snum),
1611 ippErrorString(cupsLastError())));
1616 ippDelete(response);
1619 cupsLangFree(language);
1628 /*******************************************************************
1629 * CUPS printing interface definitions...
1630 ******************************************************************/
1632 struct printif cups_printif =
1644 bool cups_pull_comment_location(TALLOC_CTX *mem_ctx,
1645 const char *printername,
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;
1659 static const char *requested[] =/* Requested attributes */
1668 DEBUG(5, ("pulling %s location\n", printername));
1671 * Make sure we don't ask for passwords...
1674 cupsSetPasswordCB(cups_passwd_cb);
1677 * Try to connect to the server...
1680 if ((http = cups_connect(frame)) == NULL) {
1686 request->request.op.operation_id = IPP_GET_PRINTER_ATTRIBUTES;
1687 request->request.op.request_id = 1;
1689 language = cupsLangDefault();
1691 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1692 "attributes-charset", NULL, "utf-8");
1694 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1695 "attributes-natural-language", NULL, language->language);
1697 if (lp_cups_server() != NULL && strlen(lp_cups_server()) > 0) {
1698 if (!push_utf8_talloc(frame, &server, lp_cups_server(), &size)) {
1702 server = talloc_strdup(frame,cupsServer());
1707 if (!push_utf8_talloc(frame, &sharename, printername, &size)) {
1710 slprintf(uri, sizeof(uri) - 1, "ipp://%s/printers/%s",
1713 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1714 "printer-uri", NULL, uri);
1716 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1717 "requested-attributes",
1718 (sizeof(requested) / sizeof(requested[0])),
1722 * Do the request and get back a response...
1725 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
1726 DEBUG(0,("Unable to get printer attributes - %s\n",
1727 ippErrorString(cupsLastError())));
1731 for (attr = response->attrs; attr != NULL;) {
1733 * Skip leading attributes until we hit a printer...
1736 while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)
1743 * Pull the needed attributes from this printer...
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,
1751 attr->values[0].string.text,
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))
1761 if (!pull_utf8_talloc(mem_ctx,
1763 attr->values[0].string.text,
1767 DEBUG(5,("cups_pull_comment_location: Using cups comment: %s\n",
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))
1775 if (!pull_utf8_talloc(mem_ctx,
1777 attr->values[0].string.text,
1781 DEBUG(5,("cups_pull_comment_location: Using cups location: %s\n",
1789 * We have everything needed...
1800 ippDelete(response);
1807 cupsLangFree(language);
1817 /* this keeps fussy compilers happy */
1818 void print_cups_dummy(void);
1819 void print_cups_dummy(void) {}
1820 #endif /* HAVE_CUPS */