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 bool cups_cache_reload(void)
111 TALLOC_CTX *frame = talloc_stackframe();
112 http_t *http = NULL; /* HTTP connection to server */
113 ipp_t *request = NULL, /* IPP Request */
114 *response = NULL; /* IPP Response */
115 ipp_attribute_t *attr; /* Current attribute */
116 cups_lang_t *language = NULL; /* Default language */
117 char *name, /* printer-name attribute */
118 *info; /* printer-info attribute */
119 static const char *requested[] =/* Requested attributes */
127 DEBUG(5, ("reloading cups printcap cache\n"));
130 * Make sure we don't ask for passwords...
133 cupsSetPasswordCB(cups_passwd_cb);
136 * Try to connect to the server...
139 if ((http = cups_connect(frame)) == NULL) {
144 * Build a CUPS_GET_PRINTERS request, which requires the following
148 * attributes-natural-language
149 * requested-attributes
154 request->request.op.operation_id = CUPS_GET_PRINTERS;
155 request->request.op.request_id = 1;
157 language = cupsLangDefault();
159 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
160 "attributes-charset", NULL, "utf-8");
162 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
163 "attributes-natural-language", NULL, language->language);
165 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
166 "requested-attributes",
167 (sizeof(requested) / sizeof(requested[0])),
171 * Do the request and get back a response...
174 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
175 DEBUG(0,("Unable to get printer list - %s\n",
176 ippErrorString(cupsLastError())));
180 for (attr = response->attrs; attr != NULL;) {
182 * Skip leading attributes until we hit a printer...
185 while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)
192 * Pull the needed attributes from this printer...
198 while (attr != NULL && attr->group_tag == IPP_TAG_PRINTER) {
199 if (strcmp(attr->name, "printer-name") == 0 &&
200 attr->value_tag == IPP_TAG_NAME) {
201 if (!pull_utf8_talloc(frame,
203 attr->values[0].string.text,
209 if (strcmp(attr->name, "printer-info") == 0 &&
210 attr->value_tag == IPP_TAG_TEXT) {
211 if (!pull_utf8_talloc(frame,
213 attr->values[0].string.text,
223 * See if we have everything needed...
229 if (!pcap_cache_add(name, info)) {
238 * Build a CUPS_GET_CLASSES request, which requires the following
242 * attributes-natural-language
243 * requested-attributes
248 request->request.op.operation_id = CUPS_GET_CLASSES;
249 request->request.op.request_id = 1;
251 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
252 "attributes-charset", NULL, "utf-8");
254 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
255 "attributes-natural-language", NULL, language->language);
257 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
258 "requested-attributes",
259 (sizeof(requested) / sizeof(requested[0])),
263 * Do the request and get back a response...
266 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
267 DEBUG(0,("Unable to get printer list - %s\n",
268 ippErrorString(cupsLastError())));
272 for (attr = response->attrs; attr != NULL;) {
274 * Skip leading attributes until we hit a printer...
277 while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)
284 * Pull the needed attributes from this printer...
290 while (attr != NULL && attr->group_tag == IPP_TAG_PRINTER) {
291 if (strcmp(attr->name, "printer-name") == 0 &&
292 attr->value_tag == IPP_TAG_NAME) {
293 if (!pull_utf8_talloc(frame,
295 attr->values[0].string.text,
301 if (strcmp(attr->name, "printer-info") == 0 &&
302 attr->value_tag == IPP_TAG_TEXT) {
303 if (!pull_utf8_talloc(frame,
305 attr->values[0].string.text,
315 * See if we have everything needed...
321 if (!pcap_cache_add(name, info)) {
333 cupsLangFree(language);
344 * 'cups_job_delete()' - Delete a job.
347 static int cups_job_delete(const char *sharename, const char *lprm_command, struct printjob *pjob)
349 TALLOC_CTX *frame = talloc_stackframe();
350 int ret = 1; /* Return value */
351 http_t *http = NULL; /* HTTP connection to server */
352 ipp_t *request = NULL, /* IPP Request */
353 *response = NULL; /* IPP Response */
354 cups_lang_t *language = NULL; /* Default language */
356 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
359 DEBUG(5,("cups_job_delete(%s, %p (%d))\n", sharename, pjob, pjob->sysjob));
362 * Make sure we don't ask for passwords...
365 cupsSetPasswordCB(cups_passwd_cb);
368 * Try to connect to the server...
371 if ((http = cups_connect(frame)) == NULL) {
376 * Build an IPP_CANCEL_JOB request, which requires the following
380 * attributes-natural-language
382 * requesting-user-name
387 request->request.op.operation_id = IPP_CANCEL_JOB;
388 request->request.op.request_id = 1;
390 language = cupsLangDefault();
392 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
393 "attributes-charset", NULL, "utf-8");
395 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
396 "attributes-natural-language", NULL, language->language);
398 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/jobs/%d", pjob->sysjob);
400 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri);
402 if (!push_utf8_talloc(frame, &user, pjob->user, &size)) {
406 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
410 * Do the request and get back a response...
413 if ((response = cupsDoRequest(http, request, "/jobs")) != NULL) {
414 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
415 DEBUG(0,("Unable to cancel job %d - %s\n", pjob->sysjob,
416 ippErrorString(cupsLastError())));
421 DEBUG(0,("Unable to cancel job %d - %s\n", pjob->sysjob,
422 ippErrorString(cupsLastError())));
430 cupsLangFree(language);
441 * 'cups_job_pause()' - Pause a job.
444 static int cups_job_pause(int snum, struct printjob *pjob)
446 TALLOC_CTX *frame = talloc_stackframe();
447 int ret = 1; /* Return value */
448 http_t *http = NULL; /* HTTP connection to server */
449 ipp_t *request = NULL, /* IPP Request */
450 *response = NULL; /* IPP Response */
451 cups_lang_t *language = NULL; /* Default language */
453 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
456 DEBUG(5,("cups_job_pause(%d, %p (%d))\n", snum, pjob, pjob->sysjob));
459 * Make sure we don't ask for passwords...
462 cupsSetPasswordCB(cups_passwd_cb);
465 * Try to connect to the server...
468 if ((http = cups_connect(frame)) == NULL) {
473 * Build an IPP_HOLD_JOB request, which requires the following
477 * attributes-natural-language
479 * requesting-user-name
484 request->request.op.operation_id = IPP_HOLD_JOB;
485 request->request.op.request_id = 1;
487 language = cupsLangDefault();
489 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
490 "attributes-charset", NULL, "utf-8");
492 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
493 "attributes-natural-language", NULL, language->language);
495 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/jobs/%d", pjob->sysjob);
497 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri);
499 if (!push_utf8_talloc(frame, &user, pjob->user, &size)) {
502 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
506 * Do the request and get back a response...
509 if ((response = cupsDoRequest(http, request, "/jobs")) != NULL) {
510 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
511 DEBUG(0,("Unable to hold job %d - %s\n", pjob->sysjob,
512 ippErrorString(cupsLastError())));
517 DEBUG(0,("Unable to hold job %d - %s\n", pjob->sysjob,
518 ippErrorString(cupsLastError())));
526 cupsLangFree(language);
537 * 'cups_job_resume()' - Resume a paused job.
540 static int cups_job_resume(int snum, struct printjob *pjob)
542 TALLOC_CTX *frame = talloc_stackframe();
543 int ret = 1; /* Return value */
544 http_t *http = NULL; /* HTTP connection to server */
545 ipp_t *request = NULL, /* IPP Request */
546 *response = NULL; /* IPP Response */
547 cups_lang_t *language = NULL; /* Default language */
549 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
552 DEBUG(5,("cups_job_resume(%d, %p (%d))\n", snum, pjob, pjob->sysjob));
555 * Make sure we don't ask for passwords...
558 cupsSetPasswordCB(cups_passwd_cb);
561 * Try to connect to the server...
564 if ((http = cups_connect(frame)) == NULL) {
569 * Build an IPP_RELEASE_JOB request, which requires the following
573 * attributes-natural-language
575 * requesting-user-name
580 request->request.op.operation_id = IPP_RELEASE_JOB;
581 request->request.op.request_id = 1;
583 language = cupsLangDefault();
585 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
586 "attributes-charset", NULL, "utf-8");
588 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
589 "attributes-natural-language", NULL, language->language);
591 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/jobs/%d", pjob->sysjob);
593 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri);
595 if (!push_utf8_talloc(frame, &user, pjob->user, &size)) {
598 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
602 * Do the request and get back a response...
605 if ((response = cupsDoRequest(http, request, "/jobs")) != NULL) {
606 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
607 DEBUG(0,("Unable to release job %d - %s\n", pjob->sysjob,
608 ippErrorString(cupsLastError())));
613 DEBUG(0,("Unable to release job %d - %s\n", pjob->sysjob,
614 ippErrorString(cupsLastError())));
622 cupsLangFree(language);
633 * 'cups_job_submit()' - Submit a job for printing.
636 static int cups_job_submit(int snum, struct printjob *pjob)
638 TALLOC_CTX *frame = talloc_stackframe();
639 int ret = 1; /* Return value */
640 http_t *http = NULL; /* HTTP connection to server */
641 ipp_t *request = NULL, /* IPP Request */
642 *response = NULL; /* IPP Response */
643 cups_lang_t *language = NULL; /* Default language */
644 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
645 const char *clientname = NULL; /* hostname of client for job-originating-host attribute */
646 char *new_jobname = NULL;
648 cups_option_t *options = NULL;
649 char *printername = NULL;
651 char *jobname = NULL;
652 char *cupsoptions = NULL;
653 char *filename = NULL;
655 char addr[INET6_ADDRSTRLEN];
657 DEBUG(5,("cups_job_submit(%d, %p (%d))\n", snum, pjob, pjob->sysjob));
660 * Make sure we don't ask for passwords...
663 cupsSetPasswordCB(cups_passwd_cb);
666 * Try to connect to the server...
669 if ((http = cups_connect(frame)) == NULL) {
674 * Build an IPP_PRINT_JOB request, which requires the following
678 * attributes-natural-language
680 * requesting-user-name
686 request->request.op.operation_id = IPP_PRINT_JOB;
687 request->request.op.request_id = 1;
689 language = cupsLangDefault();
691 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
692 "attributes-charset", NULL, "utf-8");
694 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
695 "attributes-natural-language", NULL, language->language);
697 if (!push_utf8_talloc(frame, &printername, PRINTERNAME(snum), &size)) {
700 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s",
703 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
704 "printer-uri", NULL, uri);
706 if (!push_utf8_talloc(frame, &user, pjob->user, &size)) {
709 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
712 clientname = client_name(get_client_fd());
713 if (strcmp(clientname, "UNKNOWN") == 0) {
714 clientname = client_addr(get_client_fd(),addr,sizeof(addr));
717 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
718 "job-originating-host-name", NULL,
721 if (!push_utf8_talloc(frame, &jobname, pjob->jobname, &size)) {
724 new_jobname = talloc_asprintf(frame,
725 "%s%.8u %s", PRINT_SPOOL_PREFIX,
726 (unsigned int)pjob->smbjob,
728 if (new_jobname == NULL) {
732 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name", NULL,
736 * add any options defined in smb.conf
739 if (!push_utf8_talloc(frame, &cupsoptions, lp_cups_options(snum), &size)) {
744 num_options = cupsParseOptions(cupsoptions, num_options, &options);
747 cupsEncodeOptions(request, num_options, options);
750 * Do the request and get back a response...
753 slprintf(uri, sizeof(uri) - 1, "/printers/%s", printername);
755 if (!push_utf8_talloc(frame, &filename, pjob->filename, &size)) {
758 if ((response = cupsDoFileRequest(http, request, uri, pjob->filename)) != NULL) {
759 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
760 DEBUG(0,("Unable to print file to %s - %s\n", PRINTERNAME(snum),
761 ippErrorString(cupsLastError())));
766 DEBUG(0,("Unable to print file to `%s' - %s\n", PRINTERNAME(snum),
767 ippErrorString(cupsLastError())));
771 unlink(pjob->filename);
772 /* else print_job_end will do it for us */
779 cupsLangFree(language);
790 * 'cups_queue_get()' - Get all the jobs in the print queue.
793 static int cups_queue_get(const char *sharename,
794 enum printing_types printing_type,
796 print_queue_struct **q,
797 print_status_struct *status)
799 TALLOC_CTX *frame = talloc_stackframe();
800 char *printername = NULL;
801 http_t *http = NULL; /* HTTP connection to server */
802 ipp_t *request = NULL, /* IPP Request */
803 *response = NULL; /* IPP Response */
804 ipp_attribute_t *attr = NULL; /* Current attribute */
805 cups_lang_t *language = NULL; /* Default language */
806 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
807 int qcount = 0, /* Number of active queue entries */
808 qalloc = 0; /* Number of queue entries allocated */
809 print_queue_struct *queue = NULL, /* Queue entries */
810 *temp; /* Temporary pointer for queue */
811 char *user_name = NULL, /* job-originating-user-name attribute */
812 *job_name = NULL; /* job-name attribute */
813 int job_id; /* job-id attribute */
814 int job_k_octets; /* job-k-octets attribute */
815 time_t job_time; /* time-at-creation attribute */
816 ipp_jstate_t job_status; /* job-status attribute */
817 int job_priority; /* job-priority attribute */
819 static const char *jattrs[] = /* Requested job attributes */
824 "job-originating-user-name",
829 static const char *pattrs[] = /* Requested printer attributes */
832 "printer-state-message"
837 /* HACK ALERT!!! The problem with support the 'printer name'
838 option is that we key the tdb off the sharename. So we will
839 overload the lpq_command string to pass in the printername
840 (which is basically what we do for non-cups printers ... using
841 the lpq_command to get the queue listing). */
843 if (!push_utf8_talloc(frame, &printername, lpq_command, &size)) {
846 DEBUG(5,("cups_queue_get(%s, %p, %p)\n", lpq_command, q, status));
849 * Make sure we don't ask for passwords...
852 cupsSetPasswordCB(cups_passwd_cb);
855 * Try to connect to the server...
858 if ((http = cups_connect(frame)) == NULL) {
863 * Generate the printer URI...
866 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s", printername);
869 * Build an IPP_GET_JOBS request, which requires the following
873 * attributes-natural-language
874 * requested-attributes
880 request->request.op.operation_id = IPP_GET_JOBS;
881 request->request.op.request_id = 1;
883 language = cupsLangDefault();
885 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
886 "attributes-charset", NULL, "utf-8");
888 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
889 "attributes-natural-language", NULL, language->language);
891 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
892 "requested-attributes",
893 (sizeof(jattrs) / sizeof(jattrs[0])),
896 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
897 "printer-uri", NULL, uri);
900 * Do the request and get back a response...
903 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
904 DEBUG(0,("Unable to get jobs for %s - %s\n", uri,
905 ippErrorString(cupsLastError())));
909 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
910 DEBUG(0,("Unable to get jobs for %s - %s\n", uri,
911 ippErrorString(response->request.status.status_code)));
916 * Process the jobs...
923 for (attr = response->attrs; attr != NULL; attr = attr->next) {
925 * Skip leading attributes until we hit a job...
928 while (attr != NULL && attr->group_tag != IPP_TAG_JOB)
935 * Allocate memory as needed...
937 if (qcount >= qalloc) {
940 queue = SMB_REALLOC_ARRAY(queue, print_queue_struct, qalloc);
943 DEBUG(0,("cups_queue_get: Not enough memory!"));
949 temp = queue + qcount;
950 memset(temp, 0, sizeof(print_queue_struct));
953 * Pull the needed attributes from this job...
958 job_status = IPP_JOB_PENDING;
964 while (attr != NULL && attr->group_tag == IPP_TAG_JOB) {
965 if (attr->name == NULL) {
970 if (strcmp(attr->name, "job-id") == 0 &&
971 attr->value_tag == IPP_TAG_INTEGER)
972 job_id = attr->values[0].integer;
974 if (strcmp(attr->name, "job-k-octets") == 0 &&
975 attr->value_tag == IPP_TAG_INTEGER)
976 job_k_octets = attr->values[0].integer;
978 if (strcmp(attr->name, "job-priority") == 0 &&
979 attr->value_tag == IPP_TAG_INTEGER)
980 job_priority = attr->values[0].integer;
982 if (strcmp(attr->name, "job-state") == 0 &&
983 attr->value_tag == IPP_TAG_ENUM)
984 job_status = (ipp_jstate_t)(attr->values[0].integer);
986 if (strcmp(attr->name, "time-at-creation") == 0 &&
987 attr->value_tag == IPP_TAG_INTEGER)
988 job_time = attr->values[0].integer;
990 if (strcmp(attr->name, "job-name") == 0 &&
991 attr->value_tag == IPP_TAG_NAME) {
992 if (!pull_utf8_talloc(frame,
994 attr->values[0].string.text,
1000 if (strcmp(attr->name, "job-originating-user-name") == 0 &&
1001 attr->value_tag == IPP_TAG_NAME) {
1002 if (!pull_utf8_talloc(frame,
1004 attr->values[0].string.text,
1014 * See if we have everything needed...
1017 if (user_name == NULL || job_name == NULL || job_id == 0) {
1025 temp->size = job_k_octets * 1024;
1026 temp->status = job_status == IPP_JOB_PENDING ? LPQ_QUEUED :
1027 job_status == IPP_JOB_STOPPED ? LPQ_PAUSED :
1028 job_status == IPP_JOB_HELD ? LPQ_PAUSED :
1030 temp->priority = job_priority;
1031 temp->time = job_time;
1032 strlcpy(temp->fs_user, user_name, sizeof(temp->fs_user));
1033 strlcpy(temp->fs_file, job_name, sizeof(temp->fs_file));
1041 ippDelete(response);
1045 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the
1046 * following attributes:
1048 * attributes-charset
1049 * attributes-natural-language
1050 * requested-attributes
1056 request->request.op.operation_id = IPP_GET_PRINTER_ATTRIBUTES;
1057 request->request.op.request_id = 1;
1059 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1060 "attributes-charset", NULL, "utf-8");
1062 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1063 "attributes-natural-language", NULL, language->language);
1065 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1066 "requested-attributes",
1067 (sizeof(pattrs) / sizeof(pattrs[0])),
1070 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1071 "printer-uri", NULL, uri);
1074 * Do the request and get back a response...
1077 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
1078 DEBUG(0,("Unable to get printer status for %s - %s\n", printername,
1079 ippErrorString(cupsLastError())));
1084 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1085 DEBUG(0,("Unable to get printer status for %s - %s\n", printername,
1086 ippErrorString(response->request.status.status_code)));
1092 * Get the current printer status and convert it to the SAMBA values.
1095 if ((attr = ippFindAttribute(response, "printer-state", IPP_TAG_ENUM)) != NULL) {
1096 if (attr->values[0].integer == IPP_PRINTER_STOPPED)
1097 status->status = LPSTAT_STOPPED;
1099 status->status = LPSTAT_OK;
1102 if ((attr = ippFindAttribute(response, "printer-state-message",
1103 IPP_TAG_TEXT)) != NULL) {
1105 if (!pull_utf8_talloc(frame, &msg,
1106 attr->values[0].string.text,
1110 fstrcpy(status->message, msg);
1114 * Return the job queue...
1121 ippDelete(response);
1124 cupsLangFree(language);
1135 * 'cups_queue_pause()' - Pause a print queue.
1138 static int cups_queue_pause(int snum)
1140 TALLOC_CTX *frame = talloc_stackframe();
1141 int ret = 1; /* Return value */
1142 http_t *http = NULL; /* HTTP connection to server */
1143 ipp_t *request = NULL, /* IPP Request */
1144 *response = NULL; /* IPP Response */
1145 cups_lang_t *language = NULL; /* Default language */
1146 char *printername = NULL;
1147 char *username = NULL;
1148 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
1151 DEBUG(5,("cups_queue_pause(%d)\n", snum));
1154 * Make sure we don't ask for passwords...
1157 cupsSetPasswordCB(cups_passwd_cb);
1160 * Try to connect to the server...
1163 if ((http = cups_connect(frame)) == NULL) {
1168 * Build an IPP_PAUSE_PRINTER request, which requires the following
1171 * attributes-charset
1172 * attributes-natural-language
1174 * requesting-user-name
1179 request->request.op.operation_id = IPP_PAUSE_PRINTER;
1180 request->request.op.request_id = 1;
1182 language = cupsLangDefault();
1184 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1185 "attributes-charset", NULL, "utf-8");
1187 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1188 "attributes-natural-language", NULL, language->language);
1190 if (!push_utf8_talloc(frame, &printername, PRINTERNAME(snum), &size)) {
1193 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s",
1196 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
1198 if (!push_utf8_talloc(frame, &username, current_user_info.unix_name, &size)) {
1201 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
1205 * Do the request and get back a response...
1208 if ((response = cupsDoRequest(http, request, "/admin/")) != NULL) {
1209 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1210 DEBUG(0,("Unable to pause printer %s - %s\n", PRINTERNAME(snum),
1211 ippErrorString(cupsLastError())));
1216 DEBUG(0,("Unable to pause printer %s - %s\n", PRINTERNAME(snum),
1217 ippErrorString(cupsLastError())));
1222 ippDelete(response);
1225 cupsLangFree(language);
1236 * 'cups_queue_resume()' - Restart a print queue.
1239 static int cups_queue_resume(int snum)
1241 TALLOC_CTX *frame = talloc_stackframe();
1242 int ret = 1; /* Return value */
1243 http_t *http = NULL; /* HTTP connection to server */
1244 ipp_t *request = NULL, /* IPP Request */
1245 *response = NULL; /* IPP Response */
1246 cups_lang_t *language = NULL; /* Default language */
1247 char *printername = NULL;
1248 char *username = NULL;
1249 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
1252 DEBUG(5,("cups_queue_resume(%d)\n", snum));
1255 * Make sure we don't ask for passwords...
1258 cupsSetPasswordCB(cups_passwd_cb);
1261 * Try to connect to the server...
1264 if ((http = cups_connect(frame)) == NULL) {
1269 * Build an IPP_RESUME_PRINTER request, which requires the following
1272 * attributes-charset
1273 * attributes-natural-language
1275 * requesting-user-name
1280 request->request.op.operation_id = IPP_RESUME_PRINTER;
1281 request->request.op.request_id = 1;
1283 language = cupsLangDefault();
1285 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1286 "attributes-charset", NULL, "utf-8");
1288 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1289 "attributes-natural-language", NULL, language->language);
1291 if (!push_utf8_talloc(frame, &printername, PRINTERNAME(snum), &size)) {
1294 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s",
1297 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
1299 if (!push_utf8_talloc(frame, &username, current_user_info.unix_name, &size)) {
1302 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
1306 * Do the request and get back a response...
1309 if ((response = cupsDoRequest(http, request, "/admin/")) != NULL) {
1310 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1311 DEBUG(0,("Unable to resume printer %s - %s\n", PRINTERNAME(snum),
1312 ippErrorString(cupsLastError())));
1317 DEBUG(0,("Unable to resume printer %s - %s\n", PRINTERNAME(snum),
1318 ippErrorString(cupsLastError())));
1323 ippDelete(response);
1326 cupsLangFree(language);
1335 /*******************************************************************
1336 * CUPS printing interface definitions...
1337 ******************************************************************/
1339 struct printif cups_printif =
1351 bool cups_pull_comment_location(NT_PRINTER_INFO_LEVEL_2 *printer)
1353 TALLOC_CTX *frame = talloc_stackframe();
1354 http_t *http = NULL; /* HTTP connection to server */
1355 ipp_t *request = NULL, /* IPP Request */
1356 *response = NULL; /* IPP Response */
1357 ipp_attribute_t *attr; /* Current attribute */
1358 cups_lang_t *language = NULL; /* Default language */
1359 char uri[HTTP_MAX_URI];
1360 char *server = NULL;
1361 char *sharename = NULL;
1363 static const char *requested[] =/* Requested attributes */
1372 DEBUG(5, ("pulling %s location\n", printer->sharename));
1375 * Make sure we don't ask for passwords...
1378 cupsSetPasswordCB(cups_passwd_cb);
1381 * Try to connect to the server...
1384 if ((http = cups_connect(frame)) == NULL) {
1390 request->request.op.operation_id = IPP_GET_PRINTER_ATTRIBUTES;
1391 request->request.op.request_id = 1;
1393 language = cupsLangDefault();
1395 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1396 "attributes-charset", NULL, "utf-8");
1398 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1399 "attributes-natural-language", NULL, language->language);
1401 if (lp_cups_server() != NULL && strlen(lp_cups_server()) > 0) {
1402 if (!push_utf8_talloc(frame, &server, lp_cups_server(), &size)) {
1406 server = talloc_strdup(frame,cupsServer());
1411 if (!push_utf8_talloc(frame, &sharename, printer->sharename, &size)) {
1414 slprintf(uri, sizeof(uri) - 1, "ipp://%s/printers/%s",
1417 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1418 "printer-uri", NULL, uri);
1420 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1421 "requested-attributes",
1422 (sizeof(requested) / sizeof(requested[0])),
1426 * Do the request and get back a response...
1429 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
1430 DEBUG(0,("Unable to get printer attributes - %s\n",
1431 ippErrorString(cupsLastError())));
1435 for (attr = response->attrs; attr != NULL;) {
1437 * Skip leading attributes until we hit a printer...
1440 while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)
1447 * Pull the needed attributes from this printer...
1450 while ( attr && (attr->group_tag == IPP_TAG_PRINTER) ) {
1451 if (strcmp(attr->name, "printer-name") == 0 &&
1452 attr->value_tag == IPP_TAG_NAME) {
1453 if (!pull_utf8_talloc(frame,
1455 attr->values[0].string.text,
1461 /* Grab the comment if we don't have one */
1462 if ( (strcmp(attr->name, "printer-info") == 0)
1463 && (attr->value_tag == IPP_TAG_TEXT)
1464 && !strlen(printer->comment) )
1466 char *comment = NULL;
1467 if (!pull_utf8_talloc(frame,
1469 attr->values[0].string.text,
1473 DEBUG(5,("cups_pull_comment_location: Using cups comment: %s\n",
1475 strlcpy(printer->comment,
1477 sizeof(printer->comment));
1480 /* Grab the location if we don't have one */
1481 if ( (strcmp(attr->name, "printer-location") == 0)
1482 && (attr->value_tag == IPP_TAG_TEXT)
1483 && !strlen(printer->location) )
1485 char *location = NULL;
1486 if (!pull_utf8_talloc(frame,
1488 attr->values[0].string.text,
1492 DEBUG(5,("cups_pull_comment_location: Using cups location: %s\n",
1494 strlcpy(printer->location,
1496 sizeof(printer->location));
1503 * We have everything needed...
1514 ippDelete(response);
1517 cupsLangFree(language);
1527 /* this keeps fussy compilers happy */
1528 void print_cups_dummy(void);
1529 void print_cups_dummy(void) {}
1530 #endif /* HAVE_CUPS */