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.
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(smbd_messaging_context(),
438 smbd_event_context(), true))) {
439 DEBUG(0,("cups_pcap_load_async: reinit_after_fork() failed\n"));
440 smb_panic("cups_pcap_load_async: reinit_after_fork() failed");
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(smbd_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(smbd_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, PRINTERNAME(snum), &size)) {
968 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s",
971 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
972 "printer-uri", NULL, uri);
974 if (!push_utf8_talloc(frame, &user, pjob->user, &size)) {
977 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
980 clientname = client_name(get_client_fd());
981 if (strcmp(clientname, "UNKNOWN") == 0) {
982 clientname = client_addr(get_client_fd(),addr,sizeof(addr));
985 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
986 "job-originating-host-name", NULL,
989 /* Get the jobid from the filename. */
990 jobid = print_parse_jobid(pjob->filename);
991 if (jobid == (uint32_t)-1) {
992 DEBUG(0,("cups_job_submit: failed to parse jobid from name %s\n",
997 if (!push_utf8_talloc(frame, &jobname, pjob->jobname, &size)) {
1000 new_jobname = talloc_asprintf(frame,
1001 "%s%.8u %s", PRINT_SPOOL_PREFIX,
1002 (unsigned int)jobid,
1004 if (new_jobname == NULL) {
1008 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name", NULL,
1012 * add any options defined in smb.conf
1015 if (!push_utf8_talloc(frame, &cupsoptions, lp_cups_options(snum), &size)) {
1020 num_options = cupsParseOptions(cupsoptions, num_options, &options);
1023 cupsEncodeOptions(request, num_options, options);
1026 * Do the request and get back a response...
1029 slprintf(uri, sizeof(uri) - 1, "/printers/%s", printername);
1031 if (!push_utf8_talloc(frame, &filename, pjob->filename, &size)) {
1034 if ((response = cupsDoFileRequest(http, request, uri, pjob->filename)) != NULL) {
1035 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1036 DEBUG(0,("Unable to print file to %s - %s\n", PRINTERNAME(snum),
1037 ippErrorString(cupsLastError())));
1040 attr_job_id = ippFindAttribute(response, "job-id", IPP_TAG_INTEGER);
1042 pjob->sysjob = attr_job_id->values[0].integer;
1043 DEBUG(5,("cups_job_submit: job-id %d\n", pjob->sysjob));
1045 DEBUG(0,("Missing job-id attribute in IPP response"));
1049 DEBUG(0,("Unable to print file to `%s' - %s\n", PRINTERNAME(snum),
1050 ippErrorString(cupsLastError())));
1054 unlink(pjob->filename);
1055 /* else print_job_end will do it for us */
1059 ippDelete(response);
1062 cupsLangFree(language);
1073 * 'cups_queue_get()' - Get all the jobs in the print queue.
1076 static int cups_queue_get(const char *sharename,
1077 enum printing_types printing_type,
1079 print_queue_struct **q,
1080 print_status_struct *status)
1082 TALLOC_CTX *frame = talloc_stackframe();
1083 char *printername = NULL;
1084 http_t *http = NULL; /* HTTP connection to server */
1085 ipp_t *request = NULL, /* IPP Request */
1086 *response = NULL; /* IPP Response */
1087 ipp_attribute_t *attr = NULL; /* Current attribute */
1088 cups_lang_t *language = NULL; /* Default language */
1089 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
1090 int qcount = 0, /* Number of active queue entries */
1091 qalloc = 0; /* Number of queue entries allocated */
1092 print_queue_struct *queue = NULL, /* Queue entries */
1093 *temp; /* Temporary pointer for queue */
1094 char *user_name = NULL, /* job-originating-user-name attribute */
1095 *job_name = NULL; /* job-name attribute */
1096 int job_id; /* job-id attribute */
1097 int job_k_octets; /* job-k-octets attribute */
1098 time_t job_time; /* time-at-creation attribute */
1099 ipp_jstate_t job_status; /* job-status attribute */
1100 int job_priority; /* job-priority attribute */
1102 static const char *jattrs[] = /* Requested job attributes */
1107 "job-originating-user-name",
1112 static const char *pattrs[] = /* Requested printer attributes */
1115 "printer-state-message"
1120 /* HACK ALERT!!! The problem with support the 'printer name'
1121 option is that we key the tdb off the sharename. So we will
1122 overload the lpq_command string to pass in the printername
1123 (which is basically what we do for non-cups printers ... using
1124 the lpq_command to get the queue listing). */
1126 if (!push_utf8_talloc(frame, &printername, lpq_command, &size)) {
1129 DEBUG(5,("cups_queue_get(%s, %p, %p)\n", lpq_command, q, status));
1132 * Make sure we don't ask for passwords...
1135 cupsSetPasswordCB(cups_passwd_cb);
1138 * Try to connect to the server...
1141 if ((http = cups_connect(frame)) == NULL) {
1146 * Generate the printer URI...
1149 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s", printername);
1152 * Build an IPP_GET_JOBS request, which requires the following
1155 * attributes-charset
1156 * attributes-natural-language
1157 * requested-attributes
1163 request->request.op.operation_id = IPP_GET_JOBS;
1164 request->request.op.request_id = 1;
1166 language = cupsLangDefault();
1168 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1169 "attributes-charset", NULL, "utf-8");
1171 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1172 "attributes-natural-language", NULL, language->language);
1174 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1175 "requested-attributes",
1176 (sizeof(jattrs) / sizeof(jattrs[0])),
1179 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1180 "printer-uri", NULL, uri);
1183 * Do the request and get back a response...
1186 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
1187 DEBUG(0,("Unable to get jobs for %s - %s\n", uri,
1188 ippErrorString(cupsLastError())));
1192 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1193 DEBUG(0,("Unable to get jobs for %s - %s\n", uri,
1194 ippErrorString(response->request.status.status_code)));
1199 * Process the jobs...
1206 for (attr = response->attrs; attr != NULL; attr = attr->next) {
1208 * Skip leading attributes until we hit a job...
1211 while (attr != NULL && attr->group_tag != IPP_TAG_JOB)
1218 * Allocate memory as needed...
1220 if (qcount >= qalloc) {
1223 queue = SMB_REALLOC_ARRAY(queue, print_queue_struct, qalloc);
1225 if (queue == NULL) {
1226 DEBUG(0,("cups_queue_get: Not enough memory!"));
1232 temp = queue + qcount;
1233 memset(temp, 0, sizeof(print_queue_struct));
1236 * Pull the needed attributes from this job...
1241 job_status = IPP_JOB_PENDING;
1247 while (attr != NULL && attr->group_tag == IPP_TAG_JOB) {
1248 if (attr->name == NULL) {
1253 if (strcmp(attr->name, "job-id") == 0 &&
1254 attr->value_tag == IPP_TAG_INTEGER)
1255 job_id = attr->values[0].integer;
1257 if (strcmp(attr->name, "job-k-octets") == 0 &&
1258 attr->value_tag == IPP_TAG_INTEGER)
1259 job_k_octets = attr->values[0].integer;
1261 if (strcmp(attr->name, "job-priority") == 0 &&
1262 attr->value_tag == IPP_TAG_INTEGER)
1263 job_priority = attr->values[0].integer;
1265 if (strcmp(attr->name, "job-state") == 0 &&
1266 attr->value_tag == IPP_TAG_ENUM)
1267 job_status = (ipp_jstate_t)(attr->values[0].integer);
1269 if (strcmp(attr->name, "time-at-creation") == 0 &&
1270 attr->value_tag == IPP_TAG_INTEGER)
1271 job_time = attr->values[0].integer;
1273 if (strcmp(attr->name, "job-name") == 0 &&
1274 attr->value_tag == IPP_TAG_NAME) {
1275 if (!pull_utf8_talloc(frame,
1277 attr->values[0].string.text,
1283 if (strcmp(attr->name, "job-originating-user-name") == 0 &&
1284 attr->value_tag == IPP_TAG_NAME) {
1285 if (!pull_utf8_talloc(frame,
1287 attr->values[0].string.text,
1297 * See if we have everything needed...
1300 if (user_name == NULL || job_name == NULL || job_id == 0) {
1308 temp->size = job_k_octets * 1024;
1309 temp->status = job_status == IPP_JOB_PENDING ? LPQ_QUEUED :
1310 job_status == IPP_JOB_STOPPED ? LPQ_PAUSED :
1311 job_status == IPP_JOB_HELD ? LPQ_PAUSED :
1313 temp->priority = job_priority;
1314 temp->time = job_time;
1315 strlcpy(temp->fs_user, user_name, sizeof(temp->fs_user));
1316 strlcpy(temp->fs_file, job_name, sizeof(temp->fs_file));
1324 ippDelete(response);
1328 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the
1329 * following attributes:
1331 * attributes-charset
1332 * attributes-natural-language
1333 * requested-attributes
1339 request->request.op.operation_id = IPP_GET_PRINTER_ATTRIBUTES;
1340 request->request.op.request_id = 1;
1342 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1343 "attributes-charset", NULL, "utf-8");
1345 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1346 "attributes-natural-language", NULL, language->language);
1348 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1349 "requested-attributes",
1350 (sizeof(pattrs) / sizeof(pattrs[0])),
1353 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1354 "printer-uri", NULL, uri);
1357 * Do the request and get back a response...
1360 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
1361 DEBUG(0,("Unable to get printer status for %s - %s\n", printername,
1362 ippErrorString(cupsLastError())));
1366 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1367 DEBUG(0,("Unable to get printer status for %s - %s\n", printername,
1368 ippErrorString(response->request.status.status_code)));
1373 * Get the current printer status and convert it to the SAMBA values.
1376 if ((attr = ippFindAttribute(response, "printer-state", IPP_TAG_ENUM)) != NULL) {
1377 if (attr->values[0].integer == IPP_PRINTER_STOPPED)
1378 status->status = LPSTAT_STOPPED;
1380 status->status = LPSTAT_OK;
1383 if ((attr = ippFindAttribute(response, "printer-state-message",
1384 IPP_TAG_TEXT)) != NULL) {
1386 if (!pull_utf8_talloc(frame, &msg,
1387 attr->values[0].string.text,
1393 fstrcpy(status->message, msg);
1399 * Return the job queue...
1405 ippDelete(response);
1408 cupsLangFree(language);
1419 * 'cups_queue_pause()' - Pause a print queue.
1422 static int cups_queue_pause(int snum)
1424 TALLOC_CTX *frame = talloc_stackframe();
1425 int ret = 1; /* Return value */
1426 http_t *http = NULL; /* HTTP connection to server */
1427 ipp_t *request = NULL, /* IPP Request */
1428 *response = NULL; /* IPP Response */
1429 cups_lang_t *language = NULL; /* Default language */
1430 char *printername = NULL;
1431 char *username = NULL;
1432 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
1435 DEBUG(5,("cups_queue_pause(%d)\n", snum));
1438 * Make sure we don't ask for passwords...
1441 cupsSetPasswordCB(cups_passwd_cb);
1444 * Try to connect to the server...
1447 if ((http = cups_connect(frame)) == NULL) {
1452 * Build an IPP_PAUSE_PRINTER request, which requires the following
1455 * attributes-charset
1456 * attributes-natural-language
1458 * requesting-user-name
1463 request->request.op.operation_id = IPP_PAUSE_PRINTER;
1464 request->request.op.request_id = 1;
1466 language = cupsLangDefault();
1468 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1469 "attributes-charset", NULL, "utf-8");
1471 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1472 "attributes-natural-language", NULL, language->language);
1474 if (!push_utf8_talloc(frame, &printername, PRINTERNAME(snum), &size)) {
1477 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s",
1480 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
1482 if (!push_utf8_talloc(frame, &username, current_user_info.unix_name, &size)) {
1485 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
1489 * Do the request and get back a response...
1492 if ((response = cupsDoRequest(http, request, "/admin/")) != NULL) {
1493 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1494 DEBUG(0,("Unable to pause printer %s - %s\n", PRINTERNAME(snum),
1495 ippErrorString(cupsLastError())));
1500 DEBUG(0,("Unable to pause printer %s - %s\n", PRINTERNAME(snum),
1501 ippErrorString(cupsLastError())));
1506 ippDelete(response);
1509 cupsLangFree(language);
1520 * 'cups_queue_resume()' - Restart a print queue.
1523 static int cups_queue_resume(int snum)
1525 TALLOC_CTX *frame = talloc_stackframe();
1526 int ret = 1; /* Return value */
1527 http_t *http = NULL; /* HTTP connection to server */
1528 ipp_t *request = NULL, /* IPP Request */
1529 *response = NULL; /* IPP Response */
1530 cups_lang_t *language = NULL; /* Default language */
1531 char *printername = NULL;
1532 char *username = NULL;
1533 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
1536 DEBUG(5,("cups_queue_resume(%d)\n", snum));
1539 * Make sure we don't ask for passwords...
1542 cupsSetPasswordCB(cups_passwd_cb);
1545 * Try to connect to the server...
1548 if ((http = cups_connect(frame)) == NULL) {
1553 * Build an IPP_RESUME_PRINTER request, which requires the following
1556 * attributes-charset
1557 * attributes-natural-language
1559 * requesting-user-name
1564 request->request.op.operation_id = IPP_RESUME_PRINTER;
1565 request->request.op.request_id = 1;
1567 language = cupsLangDefault();
1569 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1570 "attributes-charset", NULL, "utf-8");
1572 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1573 "attributes-natural-language", NULL, language->language);
1575 if (!push_utf8_talloc(frame, &printername, PRINTERNAME(snum), &size)) {
1578 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s",
1581 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
1583 if (!push_utf8_talloc(frame, &username, current_user_info.unix_name, &size)) {
1586 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
1590 * Do the request and get back a response...
1593 if ((response = cupsDoRequest(http, request, "/admin/")) != NULL) {
1594 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1595 DEBUG(0,("Unable to resume printer %s - %s\n", PRINTERNAME(snum),
1596 ippErrorString(cupsLastError())));
1601 DEBUG(0,("Unable to resume printer %s - %s\n", PRINTERNAME(snum),
1602 ippErrorString(cupsLastError())));
1607 ippDelete(response);
1610 cupsLangFree(language);
1619 /*******************************************************************
1620 * CUPS printing interface definitions...
1621 ******************************************************************/
1623 struct printif cups_printif =
1635 bool cups_pull_comment_location(TALLOC_CTX *mem_ctx,
1636 const char *printername,
1640 TALLOC_CTX *frame = talloc_stackframe();
1641 http_t *http = NULL; /* HTTP connection to server */
1642 ipp_t *request = NULL, /* IPP Request */
1643 *response = NULL; /* IPP Response */
1644 ipp_attribute_t *attr; /* Current attribute */
1645 cups_lang_t *language = NULL; /* Default language */
1646 char uri[HTTP_MAX_URI];
1647 char *server = NULL;
1648 char *sharename = NULL;
1650 static const char *requested[] =/* Requested attributes */
1659 DEBUG(5, ("pulling %s location\n", printername));
1662 * Make sure we don't ask for passwords...
1665 cupsSetPasswordCB(cups_passwd_cb);
1668 * Try to connect to the server...
1671 if ((http = cups_connect(frame)) == NULL) {
1677 request->request.op.operation_id = IPP_GET_PRINTER_ATTRIBUTES;
1678 request->request.op.request_id = 1;
1680 language = cupsLangDefault();
1682 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1683 "attributes-charset", NULL, "utf-8");
1685 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1686 "attributes-natural-language", NULL, language->language);
1688 if (lp_cups_server() != NULL && strlen(lp_cups_server()) > 0) {
1689 if (!push_utf8_talloc(frame, &server, lp_cups_server(), &size)) {
1693 server = talloc_strdup(frame,cupsServer());
1698 if (!push_utf8_talloc(frame, &sharename, printername, &size)) {
1701 slprintf(uri, sizeof(uri) - 1, "ipp://%s/printers/%s",
1704 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1705 "printer-uri", NULL, uri);
1707 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1708 "requested-attributes",
1709 (sizeof(requested) / sizeof(requested[0])),
1713 * Do the request and get back a response...
1716 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
1717 DEBUG(0,("Unable to get printer attributes - %s\n",
1718 ippErrorString(cupsLastError())));
1722 for (attr = response->attrs; attr != NULL;) {
1724 * Skip leading attributes until we hit a printer...
1727 while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)
1734 * Pull the needed attributes from this printer...
1737 while ( attr && (attr->group_tag == IPP_TAG_PRINTER) ) {
1738 if (strcmp(attr->name, "printer-name") == 0 &&
1739 attr->value_tag == IPP_TAG_NAME) {
1740 if (!pull_utf8_talloc(frame,
1742 attr->values[0].string.text,
1748 /* Grab the comment if we don't have one */
1749 if ( (strcmp(attr->name, "printer-info") == 0)
1750 && (attr->value_tag == IPP_TAG_TEXT))
1752 if (!pull_utf8_talloc(mem_ctx,
1754 attr->values[0].string.text,
1758 DEBUG(5,("cups_pull_comment_location: Using cups comment: %s\n",
1762 /* Grab the location if we don't have one */
1763 if ( (strcmp(attr->name, "printer-location") == 0)
1764 && (attr->value_tag == IPP_TAG_TEXT))
1766 if (!pull_utf8_talloc(mem_ctx,
1768 attr->values[0].string.text,
1772 DEBUG(5,("cups_pull_comment_location: Using cups location: %s\n",
1780 * We have everything needed...
1791 ippDelete(response);
1798 cupsLangFree(language);
1808 /* this keeps fussy compilers happy */
1809 void print_cups_dummy(void);
1810 void print_cups_dummy(void) {}
1811 #endif /* HAVE_CUPS */