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 (!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 static void cups_async_callback(struct event_context *event_ctx,
444 struct fd_event *event,
448 TALLOC_CTX *frame = talloc_stackframe();
450 struct pcap_cache *tmp_pcap_cache = NULL;
452 DEBUG(5,("cups_async_callback: callback received for printer data. "
456 char *name = NULL, *info = NULL;
457 size_t namelen = 0, infolen = 0;
460 ret = sys_read(fd, &namelen, sizeof(namelen));
465 if (ret != sizeof(namelen)) {
466 DEBUG(10,("cups_async_callback: namelen read failed %d %s\n",
467 errno, strerror(errno)));
471 DEBUG(11,("cups_async_callback: read namelen %u\n",
472 (unsigned int)namelen));
474 ret = sys_read(fd, &infolen, sizeof(infolen));
479 if (ret != sizeof(infolen)) {
480 DEBUG(10,("cups_async_callback: infolen read failed %s\n",
485 DEBUG(11,("cups_async_callback: read infolen %u\n",
486 (unsigned int)infolen));
489 name = TALLOC_ARRAY(frame, char, namelen);
493 ret = sys_read(fd, name, namelen);
498 if (ret != namelen) {
499 DEBUG(10,("cups_async_callback: name read failed %s\n",
503 DEBUG(11,("cups_async_callback: read name %s\n",
509 info = TALLOC_ARRAY(frame, char, infolen);
513 ret = sys_read(fd, info, infolen);
518 if (ret != infolen) {
519 DEBUG(10,("cups_async_callback: info read failed %s\n",
523 DEBUG(11,("cups_async_callback: read info %s\n",
529 /* Add to our local pcap cache. */
530 pcap_cache_add_specific(&tmp_pcap_cache, name, info);
536 if (tmp_pcap_cache) {
537 /* We got a namelist, replace our local cache. */
538 pcap_cache_destroy_specific(&local_pcap_copy);
539 local_pcap_copy = tmp_pcap_cache;
541 /* And the systemwide pcap cache. */
542 pcap_cache_replace(local_pcap_copy);
544 DEBUG(2,("cups_async_callback: failed to read a new "
549 TALLOC_FREE(cache_fd_event);
552 bool cups_cache_reload(void)
554 int *p_pipe_fd = TALLOC_P(NULL, int);
562 /* Set up an async refresh. */
563 if (!cups_pcap_load_async(p_pipe_fd)) {
566 if (!local_pcap_copy) {
567 /* We have no local cache, wait directly for
568 * async refresh to complete.
570 DEBUG(10,("cups_cache_reload: sync read on fd %d\n",
573 cups_async_callback(smbd_event_context(),
577 if (!local_pcap_copy) {
581 /* Replace the system cache with our
583 pcap_cache_replace(local_pcap_copy);
585 DEBUG(10,("cups_cache_reload: async read on fd %d\n",
588 /* Trigger an event when the pipe can be read. */
589 cache_fd_event = event_add_fd(smbd_event_context(),
594 if (!cache_fd_event) {
596 TALLOC_FREE(p_pipe_fd);
604 * 'cups_job_delete()' - Delete a job.
607 static int cups_job_delete(const char *sharename, const char *lprm_command, struct printjob *pjob)
609 TALLOC_CTX *frame = talloc_stackframe();
610 int ret = 1; /* Return value */
611 http_t *http = NULL; /* HTTP connection to server */
612 ipp_t *request = NULL, /* IPP Request */
613 *response = NULL; /* IPP Response */
614 cups_lang_t *language = NULL; /* Default language */
616 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
619 DEBUG(5,("cups_job_delete(%s, %p (%d))\n", sharename, pjob, pjob->sysjob));
622 * Make sure we don't ask for passwords...
625 cupsSetPasswordCB(cups_passwd_cb);
628 * Try to connect to the server...
631 if ((http = cups_connect(frame)) == NULL) {
636 * Build an IPP_CANCEL_JOB request, which requires the following
640 * attributes-natural-language
642 * requesting-user-name
647 request->request.op.operation_id = IPP_CANCEL_JOB;
648 request->request.op.request_id = 1;
650 language = cupsLangDefault();
652 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
653 "attributes-charset", NULL, "utf-8");
655 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
656 "attributes-natural-language", NULL, language->language);
658 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/jobs/%d", pjob->sysjob);
660 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri);
662 if (!push_utf8_talloc(frame, &user, pjob->user, &size)) {
666 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
670 * Do the request and get back a response...
673 if ((response = cupsDoRequest(http, request, "/jobs")) != NULL) {
674 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
675 DEBUG(0,("Unable to cancel job %d - %s\n", pjob->sysjob,
676 ippErrorString(cupsLastError())));
681 DEBUG(0,("Unable to cancel job %d - %s\n", pjob->sysjob,
682 ippErrorString(cupsLastError())));
690 cupsLangFree(language);
701 * 'cups_job_pause()' - Pause a job.
704 static int cups_job_pause(int snum, struct printjob *pjob)
706 TALLOC_CTX *frame = talloc_stackframe();
707 int ret = 1; /* Return value */
708 http_t *http = NULL; /* HTTP connection to server */
709 ipp_t *request = NULL, /* IPP Request */
710 *response = NULL; /* IPP Response */
711 cups_lang_t *language = NULL; /* Default language */
713 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
716 DEBUG(5,("cups_job_pause(%d, %p (%d))\n", snum, pjob, pjob->sysjob));
719 * Make sure we don't ask for passwords...
722 cupsSetPasswordCB(cups_passwd_cb);
725 * Try to connect to the server...
728 if ((http = cups_connect(frame)) == NULL) {
733 * Build an IPP_HOLD_JOB request, which requires the following
737 * attributes-natural-language
739 * requesting-user-name
744 request->request.op.operation_id = IPP_HOLD_JOB;
745 request->request.op.request_id = 1;
747 language = cupsLangDefault();
749 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
750 "attributes-charset", NULL, "utf-8");
752 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
753 "attributes-natural-language", NULL, language->language);
755 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/jobs/%d", pjob->sysjob);
757 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri);
759 if (!push_utf8_talloc(frame, &user, pjob->user, &size)) {
762 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
766 * Do the request and get back a response...
769 if ((response = cupsDoRequest(http, request, "/jobs")) != NULL) {
770 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
771 DEBUG(0,("Unable to hold job %d - %s\n", pjob->sysjob,
772 ippErrorString(cupsLastError())));
777 DEBUG(0,("Unable to hold job %d - %s\n", pjob->sysjob,
778 ippErrorString(cupsLastError())));
786 cupsLangFree(language);
797 * 'cups_job_resume()' - Resume a paused job.
800 static int cups_job_resume(int snum, struct printjob *pjob)
802 TALLOC_CTX *frame = talloc_stackframe();
803 int ret = 1; /* Return value */
804 http_t *http = NULL; /* HTTP connection to server */
805 ipp_t *request = NULL, /* IPP Request */
806 *response = NULL; /* IPP Response */
807 cups_lang_t *language = NULL; /* Default language */
809 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
812 DEBUG(5,("cups_job_resume(%d, %p (%d))\n", snum, pjob, pjob->sysjob));
815 * Make sure we don't ask for passwords...
818 cupsSetPasswordCB(cups_passwd_cb);
821 * Try to connect to the server...
824 if ((http = cups_connect(frame)) == NULL) {
829 * Build an IPP_RELEASE_JOB request, which requires the following
833 * attributes-natural-language
835 * requesting-user-name
840 request->request.op.operation_id = IPP_RELEASE_JOB;
841 request->request.op.request_id = 1;
843 language = cupsLangDefault();
845 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
846 "attributes-charset", NULL, "utf-8");
848 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
849 "attributes-natural-language", NULL, language->language);
851 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/jobs/%d", pjob->sysjob);
853 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri);
855 if (!push_utf8_talloc(frame, &user, pjob->user, &size)) {
858 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
862 * Do the request and get back a response...
865 if ((response = cupsDoRequest(http, request, "/jobs")) != NULL) {
866 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
867 DEBUG(0,("Unable to release job %d - %s\n", pjob->sysjob,
868 ippErrorString(cupsLastError())));
873 DEBUG(0,("Unable to release job %d - %s\n", pjob->sysjob,
874 ippErrorString(cupsLastError())));
882 cupsLangFree(language);
893 * 'cups_job_submit()' - Submit a job for printing.
896 static int cups_job_submit(int snum, struct printjob *pjob)
898 TALLOC_CTX *frame = talloc_stackframe();
899 int ret = 1; /* Return value */
900 http_t *http = NULL; /* HTTP connection to server */
901 ipp_t *request = NULL, /* IPP Request */
902 *response = NULL; /* IPP Response */
903 cups_lang_t *language = NULL; /* Default language */
904 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
905 const char *clientname = NULL; /* hostname of client for job-originating-host attribute */
906 char *new_jobname = NULL;
908 cups_option_t *options = NULL;
909 char *printername = NULL;
911 char *jobname = NULL;
912 char *cupsoptions = NULL;
913 char *filename = NULL;
915 char addr[INET6_ADDRSTRLEN];
917 DEBUG(5,("cups_job_submit(%d, %p (%d))\n", snum, pjob, pjob->sysjob));
920 * Make sure we don't ask for passwords...
923 cupsSetPasswordCB(cups_passwd_cb);
926 * Try to connect to the server...
929 if ((http = cups_connect(frame)) == NULL) {
934 * Build an IPP_PRINT_JOB request, which requires the following
938 * attributes-natural-language
940 * requesting-user-name
946 request->request.op.operation_id = IPP_PRINT_JOB;
947 request->request.op.request_id = 1;
949 language = cupsLangDefault();
951 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
952 "attributes-charset", NULL, "utf-8");
954 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
955 "attributes-natural-language", NULL, language->language);
957 if (!push_utf8_talloc(frame, &printername, PRINTERNAME(snum), &size)) {
960 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s",
963 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
964 "printer-uri", NULL, uri);
966 if (!push_utf8_talloc(frame, &user, pjob->user, &size)) {
969 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
972 clientname = client_name(get_client_fd());
973 if (strcmp(clientname, "UNKNOWN") == 0) {
974 clientname = client_addr(get_client_fd(),addr,sizeof(addr));
977 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
978 "job-originating-host-name", NULL,
981 if (!push_utf8_talloc(frame, &jobname, pjob->jobname, &size)) {
984 new_jobname = talloc_asprintf(frame,
985 "%s%.8u %s", PRINT_SPOOL_PREFIX,
986 (unsigned int)pjob->smbjob,
988 if (new_jobname == NULL) {
992 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name", NULL,
996 * add any options defined in smb.conf
999 if (!push_utf8_talloc(frame, &cupsoptions, lp_cups_options(snum), &size)) {
1004 num_options = cupsParseOptions(cupsoptions, num_options, &options);
1007 cupsEncodeOptions(request, num_options, options);
1010 * Do the request and get back a response...
1013 slprintf(uri, sizeof(uri) - 1, "/printers/%s", printername);
1015 if (!push_utf8_talloc(frame, &filename, pjob->filename, &size)) {
1018 if ((response = cupsDoFileRequest(http, request, uri, pjob->filename)) != NULL) {
1019 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1020 DEBUG(0,("Unable to print file to %s - %s\n", PRINTERNAME(snum),
1021 ippErrorString(cupsLastError())));
1026 DEBUG(0,("Unable to print file to `%s' - %s\n", PRINTERNAME(snum),
1027 ippErrorString(cupsLastError())));
1031 unlink(pjob->filename);
1032 /* else print_job_end will do it for us */
1036 ippDelete(response);
1039 cupsLangFree(language);
1050 * 'cups_queue_get()' - Get all the jobs in the print queue.
1053 static int cups_queue_get(const char *sharename,
1054 enum printing_types printing_type,
1056 print_queue_struct **q,
1057 print_status_struct *status)
1059 TALLOC_CTX *frame = talloc_stackframe();
1060 char *printername = NULL;
1061 http_t *http = NULL; /* HTTP connection to server */
1062 ipp_t *request = NULL, /* IPP Request */
1063 *response = NULL; /* IPP Response */
1064 ipp_attribute_t *attr = NULL; /* Current attribute */
1065 cups_lang_t *language = NULL; /* Default language */
1066 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
1067 int qcount = 0, /* Number of active queue entries */
1068 qalloc = 0; /* Number of queue entries allocated */
1069 print_queue_struct *queue = NULL, /* Queue entries */
1070 *temp; /* Temporary pointer for queue */
1071 char *user_name = NULL, /* job-originating-user-name attribute */
1072 *job_name = NULL; /* job-name attribute */
1073 int job_id; /* job-id attribute */
1074 int job_k_octets; /* job-k-octets attribute */
1075 time_t job_time; /* time-at-creation attribute */
1076 ipp_jstate_t job_status; /* job-status attribute */
1077 int job_priority; /* job-priority attribute */
1079 static const char *jattrs[] = /* Requested job attributes */
1084 "job-originating-user-name",
1089 static const char *pattrs[] = /* Requested printer attributes */
1092 "printer-state-message"
1097 /* HACK ALERT!!! The problem with support the 'printer name'
1098 option is that we key the tdb off the sharename. So we will
1099 overload the lpq_command string to pass in the printername
1100 (which is basically what we do for non-cups printers ... using
1101 the lpq_command to get the queue listing). */
1103 if (!push_utf8_talloc(frame, &printername, lpq_command, &size)) {
1106 DEBUG(5,("cups_queue_get(%s, %p, %p)\n", lpq_command, q, status));
1109 * Make sure we don't ask for passwords...
1112 cupsSetPasswordCB(cups_passwd_cb);
1115 * Try to connect to the server...
1118 if ((http = cups_connect(frame)) == NULL) {
1123 * Generate the printer URI...
1126 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s", printername);
1129 * Build an IPP_GET_JOBS request, which requires the following
1132 * attributes-charset
1133 * attributes-natural-language
1134 * requested-attributes
1140 request->request.op.operation_id = IPP_GET_JOBS;
1141 request->request.op.request_id = 1;
1143 language = cupsLangDefault();
1145 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1146 "attributes-charset", NULL, "utf-8");
1148 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1149 "attributes-natural-language", NULL, language->language);
1151 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1152 "requested-attributes",
1153 (sizeof(jattrs) / sizeof(jattrs[0])),
1156 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1157 "printer-uri", NULL, uri);
1160 * Do the request and get back a response...
1163 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
1164 DEBUG(0,("Unable to get jobs for %s - %s\n", uri,
1165 ippErrorString(cupsLastError())));
1169 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1170 DEBUG(0,("Unable to get jobs for %s - %s\n", uri,
1171 ippErrorString(response->request.status.status_code)));
1176 * Process the jobs...
1183 for (attr = response->attrs; attr != NULL; attr = attr->next) {
1185 * Skip leading attributes until we hit a job...
1188 while (attr != NULL && attr->group_tag != IPP_TAG_JOB)
1195 * Allocate memory as needed...
1197 if (qcount >= qalloc) {
1200 queue = SMB_REALLOC_ARRAY(queue, print_queue_struct, qalloc);
1202 if (queue == NULL) {
1203 DEBUG(0,("cups_queue_get: Not enough memory!"));
1209 temp = queue + qcount;
1210 memset(temp, 0, sizeof(print_queue_struct));
1213 * Pull the needed attributes from this job...
1218 job_status = IPP_JOB_PENDING;
1224 while (attr != NULL && attr->group_tag == IPP_TAG_JOB) {
1225 if (attr->name == NULL) {
1230 if (strcmp(attr->name, "job-id") == 0 &&
1231 attr->value_tag == IPP_TAG_INTEGER)
1232 job_id = attr->values[0].integer;
1234 if (strcmp(attr->name, "job-k-octets") == 0 &&
1235 attr->value_tag == IPP_TAG_INTEGER)
1236 job_k_octets = attr->values[0].integer;
1238 if (strcmp(attr->name, "job-priority") == 0 &&
1239 attr->value_tag == IPP_TAG_INTEGER)
1240 job_priority = attr->values[0].integer;
1242 if (strcmp(attr->name, "job-state") == 0 &&
1243 attr->value_tag == IPP_TAG_ENUM)
1244 job_status = (ipp_jstate_t)(attr->values[0].integer);
1246 if (strcmp(attr->name, "time-at-creation") == 0 &&
1247 attr->value_tag == IPP_TAG_INTEGER)
1248 job_time = attr->values[0].integer;
1250 if (strcmp(attr->name, "job-name") == 0 &&
1251 attr->value_tag == IPP_TAG_NAME) {
1252 if (!pull_utf8_talloc(frame,
1254 attr->values[0].string.text,
1260 if (strcmp(attr->name, "job-originating-user-name") == 0 &&
1261 attr->value_tag == IPP_TAG_NAME) {
1262 if (!pull_utf8_talloc(frame,
1264 attr->values[0].string.text,
1274 * See if we have everything needed...
1277 if (user_name == NULL || job_name == NULL || job_id == 0) {
1285 temp->size = job_k_octets * 1024;
1286 temp->status = job_status == IPP_JOB_PENDING ? LPQ_QUEUED :
1287 job_status == IPP_JOB_STOPPED ? LPQ_PAUSED :
1288 job_status == IPP_JOB_HELD ? LPQ_PAUSED :
1290 temp->priority = job_priority;
1291 temp->time = job_time;
1292 strlcpy(temp->fs_user, user_name, sizeof(temp->fs_user));
1293 strlcpy(temp->fs_file, job_name, sizeof(temp->fs_file));
1301 ippDelete(response);
1305 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the
1306 * following attributes:
1308 * attributes-charset
1309 * attributes-natural-language
1310 * requested-attributes
1316 request->request.op.operation_id = IPP_GET_PRINTER_ATTRIBUTES;
1317 request->request.op.request_id = 1;
1319 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1320 "attributes-charset", NULL, "utf-8");
1322 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1323 "attributes-natural-language", NULL, language->language);
1325 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1326 "requested-attributes",
1327 (sizeof(pattrs) / sizeof(pattrs[0])),
1330 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1331 "printer-uri", NULL, uri);
1334 * Do the request and get back a response...
1337 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
1338 DEBUG(0,("Unable to get printer status for %s - %s\n", printername,
1339 ippErrorString(cupsLastError())));
1344 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1345 DEBUG(0,("Unable to get printer status for %s - %s\n", printername,
1346 ippErrorString(response->request.status.status_code)));
1352 * Get the current printer status and convert it to the SAMBA values.
1355 if ((attr = ippFindAttribute(response, "printer-state", IPP_TAG_ENUM)) != NULL) {
1356 if (attr->values[0].integer == IPP_PRINTER_STOPPED)
1357 status->status = LPSTAT_STOPPED;
1359 status->status = LPSTAT_OK;
1362 if ((attr = ippFindAttribute(response, "printer-state-message",
1363 IPP_TAG_TEXT)) != NULL) {
1365 if (!pull_utf8_talloc(frame, &msg,
1366 attr->values[0].string.text,
1372 fstrcpy(status->message, msg);
1376 * Return the job queue...
1383 ippDelete(response);
1386 cupsLangFree(language);
1397 * 'cups_queue_pause()' - Pause a print queue.
1400 static int cups_queue_pause(int snum)
1402 TALLOC_CTX *frame = talloc_stackframe();
1403 int ret = 1; /* Return value */
1404 http_t *http = NULL; /* HTTP connection to server */
1405 ipp_t *request = NULL, /* IPP Request */
1406 *response = NULL; /* IPP Response */
1407 cups_lang_t *language = NULL; /* Default language */
1408 char *printername = NULL;
1409 char *username = NULL;
1410 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
1413 DEBUG(5,("cups_queue_pause(%d)\n", snum));
1416 * Make sure we don't ask for passwords...
1419 cupsSetPasswordCB(cups_passwd_cb);
1422 * Try to connect to the server...
1425 if ((http = cups_connect(frame)) == NULL) {
1430 * Build an IPP_PAUSE_PRINTER request, which requires the following
1433 * attributes-charset
1434 * attributes-natural-language
1436 * requesting-user-name
1441 request->request.op.operation_id = IPP_PAUSE_PRINTER;
1442 request->request.op.request_id = 1;
1444 language = cupsLangDefault();
1446 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1447 "attributes-charset", NULL, "utf-8");
1449 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1450 "attributes-natural-language", NULL, language->language);
1452 if (!push_utf8_talloc(frame, &printername, PRINTERNAME(snum), &size)) {
1455 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s",
1458 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
1460 if (!push_utf8_talloc(frame, &username, current_user_info.unix_name, &size)) {
1463 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
1467 * Do the request and get back a response...
1470 if ((response = cupsDoRequest(http, request, "/admin/")) != NULL) {
1471 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1472 DEBUG(0,("Unable to pause printer %s - %s\n", PRINTERNAME(snum),
1473 ippErrorString(cupsLastError())));
1478 DEBUG(0,("Unable to pause printer %s - %s\n", PRINTERNAME(snum),
1479 ippErrorString(cupsLastError())));
1484 ippDelete(response);
1487 cupsLangFree(language);
1498 * 'cups_queue_resume()' - Restart a print queue.
1501 static int cups_queue_resume(int snum)
1503 TALLOC_CTX *frame = talloc_stackframe();
1504 int ret = 1; /* Return value */
1505 http_t *http = NULL; /* HTTP connection to server */
1506 ipp_t *request = NULL, /* IPP Request */
1507 *response = NULL; /* IPP Response */
1508 cups_lang_t *language = NULL; /* Default language */
1509 char *printername = NULL;
1510 char *username = NULL;
1511 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
1514 DEBUG(5,("cups_queue_resume(%d)\n", snum));
1517 * Make sure we don't ask for passwords...
1520 cupsSetPasswordCB(cups_passwd_cb);
1523 * Try to connect to the server...
1526 if ((http = cups_connect(frame)) == NULL) {
1531 * Build an IPP_RESUME_PRINTER request, which requires the following
1534 * attributes-charset
1535 * attributes-natural-language
1537 * requesting-user-name
1542 request->request.op.operation_id = IPP_RESUME_PRINTER;
1543 request->request.op.request_id = 1;
1545 language = cupsLangDefault();
1547 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1548 "attributes-charset", NULL, "utf-8");
1550 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1551 "attributes-natural-language", NULL, language->language);
1553 if (!push_utf8_talloc(frame, &printername, PRINTERNAME(snum), &size)) {
1556 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s",
1559 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
1561 if (!push_utf8_talloc(frame, &username, current_user_info.unix_name, &size)) {
1564 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
1568 * Do the request and get back a response...
1571 if ((response = cupsDoRequest(http, request, "/admin/")) != NULL) {
1572 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1573 DEBUG(0,("Unable to resume printer %s - %s\n", PRINTERNAME(snum),
1574 ippErrorString(cupsLastError())));
1579 DEBUG(0,("Unable to resume printer %s - %s\n", PRINTERNAME(snum),
1580 ippErrorString(cupsLastError())));
1585 ippDelete(response);
1588 cupsLangFree(language);
1597 /*******************************************************************
1598 * CUPS printing interface definitions...
1599 ******************************************************************/
1601 struct printif cups_printif =
1613 bool cups_pull_comment_location(NT_PRINTER_INFO_LEVEL_2 *printer)
1615 TALLOC_CTX *frame = talloc_stackframe();
1616 http_t *http = NULL; /* HTTP connection to server */
1617 ipp_t *request = NULL, /* IPP Request */
1618 *response = NULL; /* IPP Response */
1619 ipp_attribute_t *attr; /* Current attribute */
1620 cups_lang_t *language = NULL; /* Default language */
1621 char uri[HTTP_MAX_URI];
1622 char *server = NULL;
1623 char *sharename = NULL;
1625 static const char *requested[] =/* Requested attributes */
1634 DEBUG(5, ("pulling %s location\n", printer->sharename));
1637 * Make sure we don't ask for passwords...
1640 cupsSetPasswordCB(cups_passwd_cb);
1643 * Try to connect to the server...
1646 if ((http = cups_connect(frame)) == NULL) {
1652 request->request.op.operation_id = IPP_GET_PRINTER_ATTRIBUTES;
1653 request->request.op.request_id = 1;
1655 language = cupsLangDefault();
1657 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1658 "attributes-charset", NULL, "utf-8");
1660 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1661 "attributes-natural-language", NULL, language->language);
1663 if (lp_cups_server() != NULL && strlen(lp_cups_server()) > 0) {
1664 if (!push_utf8_talloc(frame, &server, lp_cups_server(), &size)) {
1668 server = talloc_strdup(frame,cupsServer());
1673 if (!push_utf8_talloc(frame, &sharename, printer->sharename, &size)) {
1676 slprintf(uri, sizeof(uri) - 1, "ipp://%s/printers/%s",
1679 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1680 "printer-uri", NULL, uri);
1682 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1683 "requested-attributes",
1684 (sizeof(requested) / sizeof(requested[0])),
1688 * Do the request and get back a response...
1691 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
1692 DEBUG(0,("Unable to get printer attributes - %s\n",
1693 ippErrorString(cupsLastError())));
1697 for (attr = response->attrs; attr != NULL;) {
1699 * Skip leading attributes until we hit a printer...
1702 while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)
1709 * Pull the needed attributes from this printer...
1712 while ( attr && (attr->group_tag == IPP_TAG_PRINTER) ) {
1713 if (strcmp(attr->name, "printer-name") == 0 &&
1714 attr->value_tag == IPP_TAG_NAME) {
1715 if (!pull_utf8_talloc(frame,
1717 attr->values[0].string.text,
1723 /* Grab the comment if we don't have one */
1724 if ( (strcmp(attr->name, "printer-info") == 0)
1725 && (attr->value_tag == IPP_TAG_TEXT)
1726 && !strlen(printer->comment) )
1728 char *comment = NULL;
1729 if (!pull_utf8_talloc(frame,
1731 attr->values[0].string.text,
1735 DEBUG(5,("cups_pull_comment_location: Using cups comment: %s\n",
1737 strlcpy(printer->comment,
1739 sizeof(printer->comment));
1742 /* Grab the location if we don't have one */
1743 if ( (strcmp(attr->name, "printer-location") == 0)
1744 && (attr->value_tag == IPP_TAG_TEXT)
1745 && !strlen(printer->location) )
1747 char *location = NULL;
1748 if (!pull_utf8_talloc(frame,
1750 attr->values[0].string.text,
1754 DEBUG(5,("cups_pull_comment_location: Using cups location: %s\n",
1756 strlcpy(printer->location,
1758 sizeof(printer->location));
1765 * We have everything needed...
1776 ippDelete(response);
1783 cupsLangFree(language);
1793 /* this keeps fussy compilers happy */
1794 void print_cups_dummy(void);
1795 void print_cups_dummy(void) {}
1796 #endif /* HAVE_CUPS */