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.
29 #include <cups/cups.h>
30 #include <cups/language.h>
32 static SIG_ATOMIC_T gotalarm;
34 /***************************************************************
35 Signal function to tell us we timed out.
36 ****************************************************************/
38 static void gotalarm_sig(void)
43 extern userdom_struct current_user_info;
46 * 'cups_passwd_cb()' - The CUPS password callback...
49 static const char * /* O - Password or NULL */
50 cups_passwd_cb(const char *prompt) /* I - Prompt */
53 * Always return NULL to indicate that no password is available...
59 static http_t *cups_connect(TALLOC_CTX *frame)
62 char *server = NULL, *p = NULL;
64 int timeout = lp_cups_connection_timeout();
67 if (lp_cups_server() != NULL && strlen(lp_cups_server()) > 0) {
68 if (!push_utf8_talloc(frame, &server, lp_cups_server(), &size)) {
72 server = talloc_strdup(frame,cupsServer());
78 p = strchr(server, ':');
86 DEBUG(10, ("connecting to cups server %s:%d\n",
92 CatchSignal(SIGALRM, SIGNAL_CAST gotalarm_sig);
96 http = httpConnect(server, port);
98 CatchSignal(SIGALRM, SIGNAL_CAST SIG_IGN);
102 DEBUG(0,("Unable to connect to CUPS server %s:%d - %s\n",
103 server, port, strerror(errno)));
109 static void send_pcap_info(const char *name, const char *info, void *pd)
112 size_t namelen = name ? strlen(name)+1 : 0;
113 size_t infolen = info ? strlen(info)+1 : 0;
115 DEBUG(11,("send_pcap_info: writing namelen %u\n", (unsigned int)namelen));
116 if (sys_write(fd, &namelen, sizeof(namelen)) != sizeof(namelen)) {
117 DEBUG(10,("send_pcap_info: namelen write failed %s\n",
121 DEBUG(11,("send_pcap_info: writing infolen %u\n", (unsigned int)infolen));
122 if (sys_write(fd, &infolen, sizeof(infolen)) != sizeof(infolen)) {
123 DEBUG(10,("send_pcap_info: infolen write failed %s\n",
128 DEBUG(11,("send_pcap_info: writing name %s\n", name));
129 if (sys_write(fd, name, namelen) != namelen) {
130 DEBUG(10,("send_pcap_info: name write failed %s\n",
136 DEBUG(11,("send_pcap_info: writing info %s\n", info));
137 if (sys_write(fd, info, infolen) != infolen) {
138 DEBUG(10,("send_pcap_info: info write failed %s\n",
145 static bool cups_cache_reload_async(int fd)
147 TALLOC_CTX *frame = talloc_stackframe();
148 struct pcap_cache *tmp_pcap_cache = NULL;
149 http_t *http = NULL; /* HTTP connection to server */
150 ipp_t *request = NULL, /* IPP Request */
151 *response = NULL; /* IPP Response */
152 ipp_attribute_t *attr; /* Current attribute */
153 cups_lang_t *language = NULL; /* Default language */
154 char *name, /* printer-name attribute */
155 *info; /* printer-info attribute */
156 static const char *requested[] =/* Requested attributes */
164 DEBUG(5, ("reloading cups printcap cache\n"));
167 * Make sure we don't ask for passwords...
170 cupsSetPasswordCB(cups_passwd_cb);
173 * Try to connect to the server...
176 if ((http = cups_connect(frame)) == NULL) {
181 * Build a CUPS_GET_PRINTERS request, which requires the following
185 * attributes-natural-language
186 * requested-attributes
191 request->request.op.operation_id = CUPS_GET_PRINTERS;
192 request->request.op.request_id = 1;
194 language = cupsLangDefault();
196 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
197 "attributes-charset", NULL, "utf-8");
199 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
200 "attributes-natural-language", NULL, language->language);
202 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
203 "requested-attributes",
204 (sizeof(requested) / sizeof(requested[0])),
208 * Do the request and get back a response...
211 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
212 DEBUG(0,("Unable to get printer list - %s\n",
213 ippErrorString(cupsLastError())));
217 for (attr = response->attrs; attr != NULL;) {
219 * Skip leading attributes until we hit a printer...
222 while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)
229 * Pull the needed attributes from this printer...
235 while (attr != NULL && attr->group_tag == IPP_TAG_PRINTER) {
236 if (strcmp(attr->name, "printer-name") == 0 &&
237 attr->value_tag == IPP_TAG_NAME) {
238 if (!pull_utf8_talloc(frame,
240 attr->values[0].string.text,
246 if (strcmp(attr->name, "printer-info") == 0 &&
247 attr->value_tag == IPP_TAG_TEXT) {
248 if (!pull_utf8_talloc(frame,
250 attr->values[0].string.text,
260 * See if we have everything needed...
266 if (!pcap_cache_add_specific(&tmp_pcap_cache, name, info)) {
275 * Build a CUPS_GET_CLASSES request, which requires the following
279 * attributes-natural-language
280 * requested-attributes
285 request->request.op.operation_id = CUPS_GET_CLASSES;
286 request->request.op.request_id = 1;
288 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
289 "attributes-charset", NULL, "utf-8");
291 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
292 "attributes-natural-language", NULL, language->language);
294 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
295 "requested-attributes",
296 (sizeof(requested) / sizeof(requested[0])),
300 * Do the request and get back a response...
303 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
304 DEBUG(0,("Unable to get printer list - %s\n",
305 ippErrorString(cupsLastError())));
309 for (attr = response->attrs; attr != NULL;) {
311 * Skip leading attributes until we hit a printer...
314 while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)
321 * Pull the needed attributes from this printer...
327 while (attr != NULL && attr->group_tag == IPP_TAG_PRINTER) {
328 if (strcmp(attr->name, "printer-name") == 0 &&
329 attr->value_tag == IPP_TAG_NAME) {
330 if (!pull_utf8_talloc(frame,
332 attr->values[0].string.text,
338 if (strcmp(attr->name, "printer-info") == 0 &&
339 attr->value_tag == IPP_TAG_TEXT) {
340 if (!pull_utf8_talloc(frame,
342 attr->values[0].string.text,
352 * See if we have everything needed...
358 if (!pcap_cache_add_specific(&tmp_pcap_cache, name, info)) {
370 cupsLangFree(language);
375 /* Send all the entries up the pipe. */
376 if (tmp_pcap_cache) {
377 pcap_printer_fn_specific(tmp_pcap_cache,
381 pcap_cache_destroy_specific(&tmp_pcap_cache);
387 static struct pcap_cache *local_pcap_copy;
388 struct fd_event *cache_fd_event;
390 static bool cups_pcap_load_async(int *pfd)
397 if (cache_fd_event) {
398 DEBUG(3,("cups_pcap_load_async: already waiting for "
399 "a refresh event\n" ));
403 DEBUG(5,("cups_pcap_load_async: asynchronously loading cups printers\n"));
405 if (pipe(fds) == -1) {
410 if (pid == (pid_t)-1) {
411 DEBUG(10,("cups_pcap_load_async: fork failed %s\n",
419 DEBUG(10,("cups_pcap_load_async: child pid = %u\n",
420 (unsigned int)pid ));
429 close_all_print_db();
431 if (!NT_STATUS_IS_OK(reinit_after_fork(smbd_messaging_context(),
432 smbd_event_context(), true))) {
433 DEBUG(0,("cups_pcap_load_async: reinit_after_fork() failed\n"));
434 smb_panic("cups_pcap_load_async: reinit_after_fork() failed");
438 cups_cache_reload_async(fds[1]);
443 struct cups_async_cb_args {
445 void (*post_cache_fill_fn)(void);
448 static void cups_async_callback(struct event_context *event_ctx,
449 struct fd_event *event,
453 TALLOC_CTX *frame = talloc_stackframe();
454 struct cups_async_cb_args *cb_args = (struct cups_async_cb_args *)p;
455 int fd = cb_args->pipe_fd;
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 /* Caller may have requested post cache fill callback */
551 if (cb_args->post_cache_fill_fn) {
552 cb_args->post_cache_fill_fn();
555 DEBUG(2,("cups_async_callback: failed to read a new "
559 TALLOC_FREE(cb_args);
560 TALLOC_FREE(cache_fd_event);
563 bool cups_cache_reload(void (*post_cache_fill_fn)(void))
565 struct cups_async_cb_args *cb_args;
568 cb_args = TALLOC_P(NULL, struct cups_async_cb_args);
572 cb_args->post_cache_fill_fn = post_cache_fill_fn;
573 p_pipe_fd = &cb_args->pipe_fd;
576 /* Set up an async refresh. */
577 if (!cups_pcap_load_async(p_pipe_fd)) {
578 talloc_free(cb_args);
581 if (!local_pcap_copy) {
582 /* We have no local cache, wait directly for
583 * async refresh to complete.
585 DEBUG(10,("cups_cache_reload: sync read on fd %d\n",
588 cups_async_callback(smbd_event_context(),
592 if (!local_pcap_copy) {
596 /* Replace the system cache with our
598 pcap_cache_replace(local_pcap_copy);
600 DEBUG(10,("cups_cache_reload: async read on fd %d\n",
603 /* Trigger an event when the pipe can be read. */
604 cache_fd_event = event_add_fd(smbd_event_context(),
609 if (!cache_fd_event) {
611 talloc_free(cb_args);
619 * 'cups_job_delete()' - Delete a job.
622 static int cups_job_delete(const char *sharename, const char *lprm_command, struct printjob *pjob)
624 TALLOC_CTX *frame = talloc_stackframe();
625 int ret = 1; /* Return value */
626 http_t *http = NULL; /* HTTP connection to server */
627 ipp_t *request = NULL, /* IPP Request */
628 *response = NULL; /* IPP Response */
629 cups_lang_t *language = NULL; /* Default language */
631 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
634 DEBUG(5,("cups_job_delete(%s, %p (%d))\n", sharename, pjob, pjob->sysjob));
637 * Make sure we don't ask for passwords...
640 cupsSetPasswordCB(cups_passwd_cb);
643 * Try to connect to the server...
646 if ((http = cups_connect(frame)) == NULL) {
651 * Build an IPP_CANCEL_JOB request, which requires the following
655 * attributes-natural-language
657 * requesting-user-name
662 request->request.op.operation_id = IPP_CANCEL_JOB;
663 request->request.op.request_id = 1;
665 language = cupsLangDefault();
667 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
668 "attributes-charset", NULL, "utf-8");
670 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
671 "attributes-natural-language", NULL, language->language);
673 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/jobs/%d", pjob->sysjob);
675 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri);
677 if (!push_utf8_talloc(frame, &user, pjob->user, &size)) {
681 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
685 * Do the request and get back a response...
688 if ((response = cupsDoRequest(http, request, "/jobs")) != NULL) {
689 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
690 DEBUG(0,("Unable to cancel job %d - %s\n", pjob->sysjob,
691 ippErrorString(cupsLastError())));
696 DEBUG(0,("Unable to cancel job %d - %s\n", pjob->sysjob,
697 ippErrorString(cupsLastError())));
705 cupsLangFree(language);
716 * 'cups_job_pause()' - Pause a job.
719 static int cups_job_pause(int snum, struct printjob *pjob)
721 TALLOC_CTX *frame = talloc_stackframe();
722 int ret = 1; /* Return value */
723 http_t *http = NULL; /* HTTP connection to server */
724 ipp_t *request = NULL, /* IPP Request */
725 *response = NULL; /* IPP Response */
726 cups_lang_t *language = NULL; /* Default language */
728 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
731 DEBUG(5,("cups_job_pause(%d, %p (%d))\n", snum, pjob, pjob->sysjob));
734 * Make sure we don't ask for passwords...
737 cupsSetPasswordCB(cups_passwd_cb);
740 * Try to connect to the server...
743 if ((http = cups_connect(frame)) == NULL) {
748 * Build an IPP_HOLD_JOB request, which requires the following
752 * attributes-natural-language
754 * requesting-user-name
759 request->request.op.operation_id = IPP_HOLD_JOB;
760 request->request.op.request_id = 1;
762 language = cupsLangDefault();
764 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
765 "attributes-charset", NULL, "utf-8");
767 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
768 "attributes-natural-language", NULL, language->language);
770 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/jobs/%d", pjob->sysjob);
772 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri);
774 if (!push_utf8_talloc(frame, &user, pjob->user, &size)) {
777 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
781 * Do the request and get back a response...
784 if ((response = cupsDoRequest(http, request, "/jobs")) != NULL) {
785 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
786 DEBUG(0,("Unable to hold job %d - %s\n", pjob->sysjob,
787 ippErrorString(cupsLastError())));
792 DEBUG(0,("Unable to hold job %d - %s\n", pjob->sysjob,
793 ippErrorString(cupsLastError())));
801 cupsLangFree(language);
812 * 'cups_job_resume()' - Resume a paused job.
815 static int cups_job_resume(int snum, struct printjob *pjob)
817 TALLOC_CTX *frame = talloc_stackframe();
818 int ret = 1; /* Return value */
819 http_t *http = NULL; /* HTTP connection to server */
820 ipp_t *request = NULL, /* IPP Request */
821 *response = NULL; /* IPP Response */
822 cups_lang_t *language = NULL; /* Default language */
824 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
827 DEBUG(5,("cups_job_resume(%d, %p (%d))\n", snum, pjob, pjob->sysjob));
830 * Make sure we don't ask for passwords...
833 cupsSetPasswordCB(cups_passwd_cb);
836 * Try to connect to the server...
839 if ((http = cups_connect(frame)) == NULL) {
844 * Build an IPP_RELEASE_JOB request, which requires the following
848 * attributes-natural-language
850 * requesting-user-name
855 request->request.op.operation_id = IPP_RELEASE_JOB;
856 request->request.op.request_id = 1;
858 language = cupsLangDefault();
860 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
861 "attributes-charset", NULL, "utf-8");
863 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
864 "attributes-natural-language", NULL, language->language);
866 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/jobs/%d", pjob->sysjob);
868 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri);
870 if (!push_utf8_talloc(frame, &user, pjob->user, &size)) {
873 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
877 * Do the request and get back a response...
880 if ((response = cupsDoRequest(http, request, "/jobs")) != NULL) {
881 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
882 DEBUG(0,("Unable to release job %d - %s\n", pjob->sysjob,
883 ippErrorString(cupsLastError())));
888 DEBUG(0,("Unable to release job %d - %s\n", pjob->sysjob,
889 ippErrorString(cupsLastError())));
897 cupsLangFree(language);
908 * 'cups_job_submit()' - Submit a job for printing.
911 static int cups_job_submit(int snum, struct printjob *pjob)
913 TALLOC_CTX *frame = talloc_stackframe();
914 int ret = 1; /* Return value */
915 http_t *http = NULL; /* HTTP connection to server */
916 ipp_t *request = NULL, /* IPP Request */
917 *response = NULL; /* IPP Response */
918 ipp_attribute_t *attr_job_id = NULL; /* IPP Attribute "job-id" */
919 cups_lang_t *language = NULL; /* Default language */
920 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
921 const char *clientname = NULL; /* hostname of client for job-originating-host attribute */
922 char *new_jobname = NULL;
924 cups_option_t *options = NULL;
925 char *printername = NULL;
927 char *jobname = NULL;
928 char *cupsoptions = NULL;
929 char *filename = NULL;
931 uint32_t jobid = (uint32_t)-1;
932 char addr[INET6_ADDRSTRLEN];
934 DEBUG(5,("cups_job_submit(%d, %p)\n", snum, pjob));
937 * Make sure we don't ask for passwords...
940 cupsSetPasswordCB(cups_passwd_cb);
943 * Try to connect to the server...
946 if ((http = cups_connect(frame)) == NULL) {
951 * Build an IPP_PRINT_JOB request, which requires the following
955 * attributes-natural-language
957 * requesting-user-name
963 request->request.op.operation_id = IPP_PRINT_JOB;
964 request->request.op.request_id = 1;
966 language = cupsLangDefault();
968 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
969 "attributes-charset", NULL, "utf-8");
971 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
972 "attributes-natural-language", NULL, language->language);
974 if (!push_utf8_talloc(frame, &printername, PRINTERNAME(snum), &size)) {
977 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s",
980 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
981 "printer-uri", NULL, uri);
983 if (!push_utf8_talloc(frame, &user, pjob->user, &size)) {
986 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
989 clientname = client_name(get_client_fd());
990 if (strcmp(clientname, "UNKNOWN") == 0) {
991 clientname = client_addr(get_client_fd(),addr,sizeof(addr));
994 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
995 "job-originating-host-name", NULL,
998 /* Get the jobid from the filename. */
999 jobid = print_parse_jobid(pjob->filename);
1000 if (jobid == (uint32_t)-1) {
1001 DEBUG(0,("cups_job_submit: failed to parse jobid from name %s\n",
1006 if (!push_utf8_talloc(frame, &jobname, pjob->jobname, &size)) {
1009 new_jobname = talloc_asprintf(frame,
1010 "%s%.8u %s", PRINT_SPOOL_PREFIX,
1011 (unsigned int)jobid,
1013 if (new_jobname == NULL) {
1017 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name", NULL,
1021 * add any options defined in smb.conf
1024 if (!push_utf8_talloc(frame, &cupsoptions, lp_cups_options(snum), &size)) {
1029 num_options = cupsParseOptions(cupsoptions, num_options, &options);
1032 cupsEncodeOptions(request, num_options, options);
1035 * Do the request and get back a response...
1038 slprintf(uri, sizeof(uri) - 1, "/printers/%s", printername);
1040 if (!push_utf8_talloc(frame, &filename, pjob->filename, &size)) {
1043 if ((response = cupsDoFileRequest(http, request, uri, pjob->filename)) != NULL) {
1044 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1045 DEBUG(0,("Unable to print file to %s - %s\n", PRINTERNAME(snum),
1046 ippErrorString(cupsLastError())));
1049 attr_job_id = ippFindAttribute(response, "job-id", IPP_TAG_INTEGER);
1051 pjob->sysjob = attr_job_id->values[0].integer;
1052 DEBUG(5,("cups_job_submit: job-id %d\n", pjob->sysjob));
1054 DEBUG(0,("Missing job-id attribute in IPP response"));
1058 DEBUG(0,("Unable to print file to `%s' - %s\n", PRINTERNAME(snum),
1059 ippErrorString(cupsLastError())));
1063 unlink(pjob->filename);
1064 /* else print_job_end will do it for us */
1068 ippDelete(response);
1071 cupsLangFree(language);
1082 * 'cups_queue_get()' - Get all the jobs in the print queue.
1085 static int cups_queue_get(const char *sharename,
1086 enum printing_types printing_type,
1088 print_queue_struct **q,
1089 print_status_struct *status)
1091 TALLOC_CTX *frame = talloc_stackframe();
1092 char *printername = NULL;
1093 http_t *http = NULL; /* HTTP connection to server */
1094 ipp_t *request = NULL, /* IPP Request */
1095 *response = NULL; /* IPP Response */
1096 ipp_attribute_t *attr = NULL; /* Current attribute */
1097 cups_lang_t *language = NULL; /* Default language */
1098 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
1099 int qcount = 0, /* Number of active queue entries */
1100 qalloc = 0; /* Number of queue entries allocated */
1101 print_queue_struct *queue = NULL, /* Queue entries */
1102 *temp; /* Temporary pointer for queue */
1103 char *user_name = NULL, /* job-originating-user-name attribute */
1104 *job_name = NULL; /* job-name attribute */
1105 int job_id; /* job-id attribute */
1106 int job_k_octets; /* job-k-octets attribute */
1107 time_t job_time; /* time-at-creation attribute */
1108 ipp_jstate_t job_status; /* job-status attribute */
1109 int job_priority; /* job-priority attribute */
1111 static const char *jattrs[] = /* Requested job attributes */
1116 "job-originating-user-name",
1121 static const char *pattrs[] = /* Requested printer attributes */
1124 "printer-state-message"
1129 /* HACK ALERT!!! The problem with support the 'printer name'
1130 option is that we key the tdb off the sharename. So we will
1131 overload the lpq_command string to pass in the printername
1132 (which is basically what we do for non-cups printers ... using
1133 the lpq_command to get the queue listing). */
1135 if (!push_utf8_talloc(frame, &printername, lpq_command, &size)) {
1138 DEBUG(5,("cups_queue_get(%s, %p, %p)\n", lpq_command, q, status));
1141 * Make sure we don't ask for passwords...
1144 cupsSetPasswordCB(cups_passwd_cb);
1147 * Try to connect to the server...
1150 if ((http = cups_connect(frame)) == NULL) {
1155 * Generate the printer URI...
1158 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s", printername);
1161 * Build an IPP_GET_JOBS request, which requires the following
1164 * attributes-charset
1165 * attributes-natural-language
1166 * requested-attributes
1172 request->request.op.operation_id = IPP_GET_JOBS;
1173 request->request.op.request_id = 1;
1175 language = cupsLangDefault();
1177 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1178 "attributes-charset", NULL, "utf-8");
1180 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1181 "attributes-natural-language", NULL, language->language);
1183 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1184 "requested-attributes",
1185 (sizeof(jattrs) / sizeof(jattrs[0])),
1188 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1189 "printer-uri", NULL, uri);
1192 * Do the request and get back a response...
1195 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
1196 DEBUG(0,("Unable to get jobs for %s - %s\n", uri,
1197 ippErrorString(cupsLastError())));
1201 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1202 DEBUG(0,("Unable to get jobs for %s - %s\n", uri,
1203 ippErrorString(response->request.status.status_code)));
1208 * Process the jobs...
1215 for (attr = response->attrs; attr != NULL; attr = attr->next) {
1217 * Skip leading attributes until we hit a job...
1220 while (attr != NULL && attr->group_tag != IPP_TAG_JOB)
1227 * Allocate memory as needed...
1229 if (qcount >= qalloc) {
1232 queue = SMB_REALLOC_ARRAY(queue, print_queue_struct, qalloc);
1234 if (queue == NULL) {
1235 DEBUG(0,("cups_queue_get: Not enough memory!"));
1241 temp = queue + qcount;
1242 memset(temp, 0, sizeof(print_queue_struct));
1245 * Pull the needed attributes from this job...
1250 job_status = IPP_JOB_PENDING;
1256 while (attr != NULL && attr->group_tag == IPP_TAG_JOB) {
1257 if (attr->name == NULL) {
1262 if (strcmp(attr->name, "job-id") == 0 &&
1263 attr->value_tag == IPP_TAG_INTEGER)
1264 job_id = attr->values[0].integer;
1266 if (strcmp(attr->name, "job-k-octets") == 0 &&
1267 attr->value_tag == IPP_TAG_INTEGER)
1268 job_k_octets = attr->values[0].integer;
1270 if (strcmp(attr->name, "job-priority") == 0 &&
1271 attr->value_tag == IPP_TAG_INTEGER)
1272 job_priority = attr->values[0].integer;
1274 if (strcmp(attr->name, "job-state") == 0 &&
1275 attr->value_tag == IPP_TAG_ENUM)
1276 job_status = (ipp_jstate_t)(attr->values[0].integer);
1278 if (strcmp(attr->name, "time-at-creation") == 0 &&
1279 attr->value_tag == IPP_TAG_INTEGER)
1280 job_time = attr->values[0].integer;
1282 if (strcmp(attr->name, "job-name") == 0 &&
1283 attr->value_tag == IPP_TAG_NAME) {
1284 if (!pull_utf8_talloc(frame,
1286 attr->values[0].string.text,
1292 if (strcmp(attr->name, "job-originating-user-name") == 0 &&
1293 attr->value_tag == IPP_TAG_NAME) {
1294 if (!pull_utf8_talloc(frame,
1296 attr->values[0].string.text,
1306 * See if we have everything needed...
1309 if (user_name == NULL || job_name == NULL || job_id == 0) {
1317 temp->size = job_k_octets * 1024;
1318 temp->status = job_status == IPP_JOB_PENDING ? LPQ_QUEUED :
1319 job_status == IPP_JOB_STOPPED ? LPQ_PAUSED :
1320 job_status == IPP_JOB_HELD ? LPQ_PAUSED :
1322 temp->priority = job_priority;
1323 temp->time = job_time;
1324 strlcpy(temp->fs_user, user_name, sizeof(temp->fs_user));
1325 strlcpy(temp->fs_file, job_name, sizeof(temp->fs_file));
1333 ippDelete(response);
1337 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the
1338 * following attributes:
1340 * attributes-charset
1341 * attributes-natural-language
1342 * requested-attributes
1348 request->request.op.operation_id = IPP_GET_PRINTER_ATTRIBUTES;
1349 request->request.op.request_id = 1;
1351 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1352 "attributes-charset", NULL, "utf-8");
1354 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1355 "attributes-natural-language", NULL, language->language);
1357 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1358 "requested-attributes",
1359 (sizeof(pattrs) / sizeof(pattrs[0])),
1362 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1363 "printer-uri", NULL, uri);
1366 * Do the request and get back a response...
1369 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
1370 DEBUG(0,("Unable to get printer status for %s - %s\n", printername,
1371 ippErrorString(cupsLastError())));
1376 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1377 DEBUG(0,("Unable to get printer status for %s - %s\n", printername,
1378 ippErrorString(response->request.status.status_code)));
1384 * Get the current printer status and convert it to the SAMBA values.
1387 if ((attr = ippFindAttribute(response, "printer-state", IPP_TAG_ENUM)) != NULL) {
1388 if (attr->values[0].integer == IPP_PRINTER_STOPPED)
1389 status->status = LPSTAT_STOPPED;
1391 status->status = LPSTAT_OK;
1394 if ((attr = ippFindAttribute(response, "printer-state-message",
1395 IPP_TAG_TEXT)) != NULL) {
1397 if (!pull_utf8_talloc(frame, &msg,
1398 attr->values[0].string.text,
1404 fstrcpy(status->message, msg);
1408 * Return the job queue...
1415 ippDelete(response);
1418 cupsLangFree(language);
1429 * 'cups_queue_pause()' - Pause a print queue.
1432 static int cups_queue_pause(int snum)
1434 TALLOC_CTX *frame = talloc_stackframe();
1435 int ret = 1; /* Return value */
1436 http_t *http = NULL; /* HTTP connection to server */
1437 ipp_t *request = NULL, /* IPP Request */
1438 *response = NULL; /* IPP Response */
1439 cups_lang_t *language = NULL; /* Default language */
1440 char *printername = NULL;
1441 char *username = NULL;
1442 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
1445 DEBUG(5,("cups_queue_pause(%d)\n", snum));
1448 * Make sure we don't ask for passwords...
1451 cupsSetPasswordCB(cups_passwd_cb);
1454 * Try to connect to the server...
1457 if ((http = cups_connect(frame)) == NULL) {
1462 * Build an IPP_PAUSE_PRINTER request, which requires the following
1465 * attributes-charset
1466 * attributes-natural-language
1468 * requesting-user-name
1473 request->request.op.operation_id = IPP_PAUSE_PRINTER;
1474 request->request.op.request_id = 1;
1476 language = cupsLangDefault();
1478 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1479 "attributes-charset", NULL, "utf-8");
1481 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1482 "attributes-natural-language", NULL, language->language);
1484 if (!push_utf8_talloc(frame, &printername, PRINTERNAME(snum), &size)) {
1487 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s",
1490 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
1492 if (!push_utf8_talloc(frame, &username, current_user_info.unix_name, &size)) {
1495 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
1499 * Do the request and get back a response...
1502 if ((response = cupsDoRequest(http, request, "/admin/")) != NULL) {
1503 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1504 DEBUG(0,("Unable to pause printer %s - %s\n", PRINTERNAME(snum),
1505 ippErrorString(cupsLastError())));
1510 DEBUG(0,("Unable to pause printer %s - %s\n", PRINTERNAME(snum),
1511 ippErrorString(cupsLastError())));
1516 ippDelete(response);
1519 cupsLangFree(language);
1530 * 'cups_queue_resume()' - Restart a print queue.
1533 static int cups_queue_resume(int snum)
1535 TALLOC_CTX *frame = talloc_stackframe();
1536 int ret = 1; /* Return value */
1537 http_t *http = NULL; /* HTTP connection to server */
1538 ipp_t *request = NULL, /* IPP Request */
1539 *response = NULL; /* IPP Response */
1540 cups_lang_t *language = NULL; /* Default language */
1541 char *printername = NULL;
1542 char *username = NULL;
1543 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
1546 DEBUG(5,("cups_queue_resume(%d)\n", snum));
1549 * Make sure we don't ask for passwords...
1552 cupsSetPasswordCB(cups_passwd_cb);
1555 * Try to connect to the server...
1558 if ((http = cups_connect(frame)) == NULL) {
1563 * Build an IPP_RESUME_PRINTER request, which requires the following
1566 * attributes-charset
1567 * attributes-natural-language
1569 * requesting-user-name
1574 request->request.op.operation_id = IPP_RESUME_PRINTER;
1575 request->request.op.request_id = 1;
1577 language = cupsLangDefault();
1579 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1580 "attributes-charset", NULL, "utf-8");
1582 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1583 "attributes-natural-language", NULL, language->language);
1585 if (!push_utf8_talloc(frame, &printername, PRINTERNAME(snum), &size)) {
1588 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s",
1591 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
1593 if (!push_utf8_talloc(frame, &username, current_user_info.unix_name, &size)) {
1596 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
1600 * Do the request and get back a response...
1603 if ((response = cupsDoRequest(http, request, "/admin/")) != NULL) {
1604 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1605 DEBUG(0,("Unable to resume printer %s - %s\n", PRINTERNAME(snum),
1606 ippErrorString(cupsLastError())));
1611 DEBUG(0,("Unable to resume printer %s - %s\n", PRINTERNAME(snum),
1612 ippErrorString(cupsLastError())));
1617 ippDelete(response);
1620 cupsLangFree(language);
1629 /*******************************************************************
1630 * CUPS printing interface definitions...
1631 ******************************************************************/
1633 struct printif cups_printif =
1645 bool cups_pull_comment_location(NT_PRINTER_INFO_LEVEL_2 *printer)
1647 TALLOC_CTX *frame = talloc_stackframe();
1648 http_t *http = NULL; /* HTTP connection to server */
1649 ipp_t *request = NULL, /* IPP Request */
1650 *response = NULL; /* IPP Response */
1651 ipp_attribute_t *attr; /* Current attribute */
1652 cups_lang_t *language = NULL; /* Default language */
1653 char uri[HTTP_MAX_URI];
1654 char *server = NULL;
1655 char *sharename = NULL;
1657 static const char *requested[] =/* Requested attributes */
1666 DEBUG(5, ("pulling %s location\n", printer->sharename));
1669 * Make sure we don't ask for passwords...
1672 cupsSetPasswordCB(cups_passwd_cb);
1675 * Try to connect to the server...
1678 if ((http = cups_connect(frame)) == NULL) {
1684 request->request.op.operation_id = IPP_GET_PRINTER_ATTRIBUTES;
1685 request->request.op.request_id = 1;
1687 language = cupsLangDefault();
1689 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1690 "attributes-charset", NULL, "utf-8");
1692 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1693 "attributes-natural-language", NULL, language->language);
1695 if (lp_cups_server() != NULL && strlen(lp_cups_server()) > 0) {
1696 if (!push_utf8_talloc(frame, &server, lp_cups_server(), &size)) {
1700 server = talloc_strdup(frame,cupsServer());
1705 if (!push_utf8_talloc(frame, &sharename, printer->sharename, &size)) {
1708 slprintf(uri, sizeof(uri) - 1, "ipp://%s/printers/%s",
1711 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1712 "printer-uri", NULL, uri);
1714 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1715 "requested-attributes",
1716 (sizeof(requested) / sizeof(requested[0])),
1720 * Do the request and get back a response...
1723 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
1724 DEBUG(0,("Unable to get printer attributes - %s\n",
1725 ippErrorString(cupsLastError())));
1729 for (attr = response->attrs; attr != NULL;) {
1731 * Skip leading attributes until we hit a printer...
1734 while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)
1741 * Pull the needed attributes from this printer...
1744 while ( attr && (attr->group_tag == IPP_TAG_PRINTER) ) {
1745 if (strcmp(attr->name, "printer-name") == 0 &&
1746 attr->value_tag == IPP_TAG_NAME) {
1747 if (!pull_utf8_talloc(frame,
1749 attr->values[0].string.text,
1755 /* Grab the comment if we don't have one */
1756 if ( (strcmp(attr->name, "printer-info") == 0)
1757 && (attr->value_tag == IPP_TAG_TEXT)
1758 && !strlen(printer->comment) )
1760 char *comment = NULL;
1761 if (!pull_utf8_talloc(frame,
1763 attr->values[0].string.text,
1767 DEBUG(5,("cups_pull_comment_location: Using cups comment: %s\n",
1769 strlcpy(printer->comment,
1771 sizeof(printer->comment));
1774 /* Grab the location if we don't have one */
1775 if ( (strcmp(attr->name, "printer-location") == 0)
1776 && (attr->value_tag == IPP_TAG_TEXT)
1777 && !strlen(printer->location) )
1779 char *location = NULL;
1780 if (!pull_utf8_talloc(frame,
1782 attr->values[0].string.text,
1786 DEBUG(5,("cups_pull_comment_location: Using cups location: %s\n",
1788 strlcpy(printer->location,
1790 sizeof(printer->location));
1797 * We have everything needed...
1808 ippDelete(response);
1815 cupsLangFree(language);
1825 /* this keeps fussy compilers happy */
1826 void print_cups_dummy(void);
1827 void print_cups_dummy(void) {}
1828 #endif /* HAVE_CUPS */