Make use of ZERO_STRUCT instead of memset in namequery.c
[Samba.git] / source / printing / print_cups.c
blob593c5c7a1f60e06dc6abdf52a5ed3a106f57c890
1 /*
2 * Support code for the Common UNIX Printing System ("CUPS")
4 * Copyright 1999-2003 by Michael R Sweet.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
21 #include "printing.h"
23 #ifdef HAVE_CUPS
24 #include <cups/cups.h>
25 #include <cups/language.h>
27 extern userdom_struct current_user_info;
30 * 'cups_passwd_cb()' - The CUPS password callback...
33 static const char * /* O - Password or NULL */
34 cups_passwd_cb(const char *prompt) /* I - Prompt */
37 * Always return NULL to indicate that no password is available...
40 return (NULL);
43 static http_t *cups_connect(void)
45 http_t *http;
46 char *server, *p;
47 int port;
49 if (lp_cups_server() != NULL && strlen(lp_cups_server()) > 0) {
50 server = smb_xstrdup(lp_cups_server());
51 } else {
52 server = smb_xstrdup(cupsServer());
55 p = strchr(server, ':');
56 if (p) {
57 port = atoi(p+1);
58 *p = '\0';
59 } else {
60 port = ippPort();
63 DEBUG(10, ("connecting to cups server %s:%d\n",
64 server, port));
66 if ((http = httpConnect(server, port)) == NULL) {
67 DEBUG(0,("Unable to connect to CUPS server %s:%d - %s\n",
68 server, port, strerror(errno)));
69 SAFE_FREE(server);
70 return NULL;
73 SAFE_FREE(server);
74 return http;
77 bool cups_cache_reload(void)
79 http_t *http = NULL; /* HTTP connection to server */
80 ipp_t *request = NULL, /* IPP Request */
81 *response = NULL; /* IPP Response */
82 ipp_attribute_t *attr; /* Current attribute */
83 cups_lang_t *language = NULL; /* Default language */
84 char *name, /* printer-name attribute */
85 *info; /* printer-info attribute */
86 static const char *requested[] =/* Requested attributes */
88 "printer-name",
89 "printer-info"
90 };
91 bool ret = False;
93 DEBUG(5, ("reloading cups printcap cache\n"));
96 * Make sure we don't ask for passwords...
99 cupsSetPasswordCB(cups_passwd_cb);
102 * Try to connect to the server...
105 if ((http = cups_connect()) == NULL) {
106 goto out;
110 * Build a CUPS_GET_PRINTERS request, which requires the following
111 * attributes:
113 * attributes-charset
114 * attributes-natural-language
115 * requested-attributes
118 request = ippNew();
120 request->request.op.operation_id = CUPS_GET_PRINTERS;
121 request->request.op.request_id = 1;
123 language = cupsLangDefault();
125 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
126 "attributes-charset", NULL, cupsLangEncoding(language));
128 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
129 "attributes-natural-language", NULL, language->language);
131 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
132 "requested-attributes",
133 (sizeof(requested) / sizeof(requested[0])),
134 NULL, requested);
137 * Do the request and get back a response...
140 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
141 DEBUG(0,("Unable to get printer list - %s\n",
142 ippErrorString(cupsLastError())));
143 goto out;
146 for (attr = response->attrs; attr != NULL;) {
148 * Skip leading attributes until we hit a printer...
151 while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)
152 attr = attr->next;
154 if (attr == NULL)
155 break;
158 * Pull the needed attributes from this printer...
161 name = NULL;
162 info = NULL;
164 while (attr != NULL && attr->group_tag == IPP_TAG_PRINTER) {
165 if (strcmp(attr->name, "printer-name") == 0 &&
166 attr->value_tag == IPP_TAG_NAME)
167 name = attr->values[0].string.text;
169 if (strcmp(attr->name, "printer-info") == 0 &&
170 attr->value_tag == IPP_TAG_TEXT)
171 info = attr->values[0].string.text;
173 attr = attr->next;
177 * See if we have everything needed...
180 if (name == NULL)
181 break;
183 if (!pcap_cache_add(name, info)) {
184 goto out;
188 ippDelete(response);
189 response = NULL;
192 * Build a CUPS_GET_CLASSES request, which requires the following
193 * attributes:
195 * attributes-charset
196 * attributes-natural-language
197 * requested-attributes
200 request = ippNew();
202 request->request.op.operation_id = CUPS_GET_CLASSES;
203 request->request.op.request_id = 1;
205 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
206 "attributes-charset", NULL, cupsLangEncoding(language));
208 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
209 "attributes-natural-language", NULL, language->language);
211 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
212 "requested-attributes",
213 (sizeof(requested) / sizeof(requested[0])),
214 NULL, requested);
217 * Do the request and get back a response...
220 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
221 DEBUG(0,("Unable to get printer list - %s\n",
222 ippErrorString(cupsLastError())));
223 goto out;
226 for (attr = response->attrs; attr != NULL;) {
228 * Skip leading attributes until we hit a printer...
231 while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)
232 attr = attr->next;
234 if (attr == NULL)
235 break;
238 * Pull the needed attributes from this printer...
241 name = NULL;
242 info = NULL;
244 while (attr != NULL && attr->group_tag == IPP_TAG_PRINTER) {
245 if (strcmp(attr->name, "printer-name") == 0 &&
246 attr->value_tag == IPP_TAG_NAME)
247 name = attr->values[0].string.text;
249 if (strcmp(attr->name, "printer-info") == 0 &&
250 attr->value_tag == IPP_TAG_TEXT)
251 info = attr->values[0].string.text;
253 attr = attr->next;
257 * See if we have everything needed...
260 if (name == NULL)
261 break;
263 if (!pcap_cache_add(name, info)) {
264 goto out;
268 ret = True;
270 out:
271 if (response)
272 ippDelete(response);
274 if (language)
275 cupsLangFree(language);
277 if (http)
278 httpClose(http);
280 return ret;
285 * 'cups_job_delete()' - Delete a job.
288 static int cups_job_delete(const char *sharename, const char *lprm_command, struct printjob *pjob)
290 int ret = 1; /* Return value */
291 http_t *http = NULL; /* HTTP connection to server */
292 ipp_t *request = NULL, /* IPP Request */
293 *response = NULL; /* IPP Response */
294 cups_lang_t *language = NULL; /* Default language */
295 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
298 DEBUG(5,("cups_job_delete(%s, %p (%d))\n", sharename, pjob, pjob->sysjob));
301 * Make sure we don't ask for passwords...
304 cupsSetPasswordCB(cups_passwd_cb);
307 * Try to connect to the server...
310 if ((http = cups_connect()) == NULL) {
311 goto out;
315 * Build an IPP_CANCEL_JOB request, which requires the following
316 * attributes:
318 * attributes-charset
319 * attributes-natural-language
320 * job-uri
321 * requesting-user-name
324 request = ippNew();
326 request->request.op.operation_id = IPP_CANCEL_JOB;
327 request->request.op.request_id = 1;
329 language = cupsLangDefault();
331 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
332 "attributes-charset", NULL, cupsLangEncoding(language));
334 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
335 "attributes-natural-language", NULL, language->language);
337 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/jobs/%d", pjob->sysjob);
339 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri);
341 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
342 NULL, pjob->user);
345 * Do the request and get back a response...
348 if ((response = cupsDoRequest(http, request, "/jobs")) != NULL) {
349 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
350 DEBUG(0,("Unable to cancel job %d - %s\n", pjob->sysjob,
351 ippErrorString(cupsLastError())));
352 } else {
353 ret = 0;
355 } else {
356 DEBUG(0,("Unable to cancel job %d - %s\n", pjob->sysjob,
357 ippErrorString(cupsLastError())));
360 out:
361 if (response)
362 ippDelete(response);
364 if (language)
365 cupsLangFree(language);
367 if (http)
368 httpClose(http);
370 return ret;
375 * 'cups_job_pause()' - Pause a job.
378 static int cups_job_pause(int snum, struct printjob *pjob)
380 int ret = 1; /* Return value */
381 http_t *http = NULL; /* HTTP connection to server */
382 ipp_t *request = NULL, /* IPP Request */
383 *response = NULL; /* IPP Response */
384 cups_lang_t *language = NULL; /* Default language */
385 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
388 DEBUG(5,("cups_job_pause(%d, %p (%d))\n", snum, pjob, pjob->sysjob));
391 * Make sure we don't ask for passwords...
394 cupsSetPasswordCB(cups_passwd_cb);
397 * Try to connect to the server...
400 if ((http = cups_connect()) == NULL) {
401 goto out;
405 * Build an IPP_HOLD_JOB request, which requires the following
406 * attributes:
408 * attributes-charset
409 * attributes-natural-language
410 * job-uri
411 * requesting-user-name
414 request = ippNew();
416 request->request.op.operation_id = IPP_HOLD_JOB;
417 request->request.op.request_id = 1;
419 language = cupsLangDefault();
421 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
422 "attributes-charset", NULL, cupsLangEncoding(language));
424 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
425 "attributes-natural-language", NULL, language->language);
427 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/jobs/%d", pjob->sysjob);
429 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri);
431 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
432 NULL, pjob->user);
435 * Do the request and get back a response...
438 if ((response = cupsDoRequest(http, request, "/jobs")) != NULL) {
439 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
440 DEBUG(0,("Unable to hold job %d - %s\n", pjob->sysjob,
441 ippErrorString(cupsLastError())));
442 } else {
443 ret = 0;
445 } else {
446 DEBUG(0,("Unable to hold job %d - %s\n", pjob->sysjob,
447 ippErrorString(cupsLastError())));
450 out:
451 if (response)
452 ippDelete(response);
454 if (language)
455 cupsLangFree(language);
457 if (http)
458 httpClose(http);
460 return ret;
465 * 'cups_job_resume()' - Resume a paused job.
468 static int cups_job_resume(int snum, struct printjob *pjob)
470 int ret = 1; /* Return value */
471 http_t *http = NULL; /* HTTP connection to server */
472 ipp_t *request = NULL, /* IPP Request */
473 *response = NULL; /* IPP Response */
474 cups_lang_t *language = NULL; /* Default language */
475 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
478 DEBUG(5,("cups_job_resume(%d, %p (%d))\n", snum, pjob, pjob->sysjob));
481 * Make sure we don't ask for passwords...
484 cupsSetPasswordCB(cups_passwd_cb);
487 * Try to connect to the server...
490 if ((http = cups_connect()) == NULL) {
491 goto out;
495 * Build an IPP_RELEASE_JOB request, which requires the following
496 * attributes:
498 * attributes-charset
499 * attributes-natural-language
500 * job-uri
501 * requesting-user-name
504 request = ippNew();
506 request->request.op.operation_id = IPP_RELEASE_JOB;
507 request->request.op.request_id = 1;
509 language = cupsLangDefault();
511 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
512 "attributes-charset", NULL, cupsLangEncoding(language));
514 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
515 "attributes-natural-language", NULL, language->language);
517 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/jobs/%d", pjob->sysjob);
519 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri);
521 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
522 NULL, pjob->user);
525 * Do the request and get back a response...
528 if ((response = cupsDoRequest(http, request, "/jobs")) != NULL) {
529 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
530 DEBUG(0,("Unable to release job %d - %s\n", pjob->sysjob,
531 ippErrorString(cupsLastError())));
532 } else {
533 ret = 0;
535 } else {
536 DEBUG(0,("Unable to release job %d - %s\n", pjob->sysjob,
537 ippErrorString(cupsLastError())));
540 out:
541 if (response)
542 ippDelete(response);
544 if (language)
545 cupsLangFree(language);
547 if (http)
548 httpClose(http);
550 return ret;
555 * 'cups_job_submit()' - Submit a job for printing.
558 static int cups_job_submit(int snum, struct printjob *pjob)
560 int ret = 1; /* Return value */
561 http_t *http = NULL; /* HTTP connection to server */
562 ipp_t *request = NULL, /* IPP Request */
563 *response = NULL; /* IPP Response */
564 cups_lang_t *language = NULL; /* Default language */
565 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
566 const char *clientname = NULL; /* hostname of client for job-originating-host attribute */
567 char *new_jobname = NULL;
568 int num_options = 0;
569 cups_option_t *options = NULL;
570 char addr[INET6_ADDRSTRLEN];
572 DEBUG(5,("cups_job_submit(%d, %p (%d))\n", snum, pjob, pjob->sysjob));
575 * Make sure we don't ask for passwords...
578 cupsSetPasswordCB(cups_passwd_cb);
581 * Try to connect to the server...
584 if ((http = cups_connect()) == NULL) {
585 goto out;
589 * Build an IPP_PRINT_JOB request, which requires the following
590 * attributes:
592 * attributes-charset
593 * attributes-natural-language
594 * printer-uri
595 * requesting-user-name
596 * [document-data]
599 request = ippNew();
601 request->request.op.operation_id = IPP_PRINT_JOB;
602 request->request.op.request_id = 1;
604 language = cupsLangDefault();
606 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
607 "attributes-charset", NULL, cupsLangEncoding(language));
609 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
610 "attributes-natural-language", NULL, language->language);
612 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s",
613 PRINTERNAME(snum));
615 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
616 "printer-uri", NULL, uri);
618 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
619 NULL, pjob->user);
621 clientname = client_name(get_client_fd());
622 if (strcmp(clientname, "UNKNOWN") == 0) {
623 clientname = client_addr(get_client_fd(),addr,sizeof(addr));
626 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
627 "job-originating-host-name", NULL,
628 clientname);
630 if (asprintf(&new_jobname,"%s%.8u %s", PRINT_SPOOL_PREFIX,
631 (unsigned int)pjob->smbjob, pjob->jobname) < 0) {
632 goto out;
635 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name", NULL,
636 new_jobname);
639 * add any options defined in smb.conf
642 num_options = 0;
643 options = NULL;
644 num_options = cupsParseOptions(lp_cups_options(snum), num_options, &options);
646 if ( num_options )
647 cupsEncodeOptions(request, num_options, options);
650 * Do the request and get back a response...
653 slprintf(uri, sizeof(uri) - 1, "/printers/%s", PRINTERNAME(snum));
655 if ((response = cupsDoFileRequest(http, request, uri, pjob->filename)) != NULL) {
656 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
657 DEBUG(0,("Unable to print file to %s - %s\n", PRINTERNAME(snum),
658 ippErrorString(cupsLastError())));
659 } else {
660 ret = 0;
662 } else {
663 DEBUG(0,("Unable to print file to `%s' - %s\n", PRINTERNAME(snum),
664 ippErrorString(cupsLastError())));
667 if ( ret == 0 )
668 unlink(pjob->filename);
669 /* else print_job_end will do it for us */
671 out:
672 if (response)
673 ippDelete(response);
675 if (language)
676 cupsLangFree(language);
678 if (http)
679 httpClose(http);
681 SAFE_FREE(new_jobname);
683 return ret;
687 * 'cups_queue_get()' - Get all the jobs in the print queue.
690 static int cups_queue_get(const char *sharename,
691 enum printing_types printing_type,
692 char *lpq_command,
693 print_queue_struct **q,
694 print_status_struct *status)
696 fstring printername;
697 http_t *http = NULL; /* HTTP connection to server */
698 ipp_t *request = NULL, /* IPP Request */
699 *response = NULL; /* IPP Response */
700 ipp_attribute_t *attr = NULL; /* Current attribute */
701 cups_lang_t *language = NULL; /* Default language */
702 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
703 int qcount = 0, /* Number of active queue entries */
704 qalloc = 0; /* Number of queue entries allocated */
705 print_queue_struct *queue = NULL, /* Queue entries */
706 *temp; /* Temporary pointer for queue */
707 const char *user_name, /* job-originating-user-name attribute */
708 *job_name; /* job-name attribute */
709 int job_id; /* job-id attribute */
710 int job_k_octets; /* job-k-octets attribute */
711 time_t job_time; /* time-at-creation attribute */
712 ipp_jstate_t job_status; /* job-status attribute */
713 int job_priority; /* job-priority attribute */
714 static const char *jattrs[] = /* Requested job attributes */
716 "job-id",
717 "job-k-octets",
718 "job-name",
719 "job-originating-user-name",
720 "job-priority",
721 "job-state",
722 "time-at-creation",
724 static const char *pattrs[] = /* Requested printer attributes */
726 "printer-state",
727 "printer-state-message"
730 *q = NULL;
732 /* HACK ALERT!!! The problem with support the 'printer name'
733 option is that we key the tdb off the sharename. So we will
734 overload the lpq_command string to pass in the printername
735 (which is basically what we do for non-cups printers ... using
736 the lpq_command to get the queue listing). */
738 fstrcpy( printername, lpq_command );
740 DEBUG(5,("cups_queue_get(%s, %p, %p)\n", printername, q, status));
743 * Make sure we don't ask for passwords...
746 cupsSetPasswordCB(cups_passwd_cb);
749 * Try to connect to the server...
752 if ((http = cups_connect()) == NULL) {
753 goto out;
757 * Generate the printer URI...
760 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s", printername);
763 * Build an IPP_GET_JOBS request, which requires the following
764 * attributes:
766 * attributes-charset
767 * attributes-natural-language
768 * requested-attributes
769 * printer-uri
772 request = ippNew();
774 request->request.op.operation_id = IPP_GET_JOBS;
775 request->request.op.request_id = 1;
777 language = cupsLangDefault();
779 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
780 "attributes-charset", NULL, cupsLangEncoding(language));
782 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
783 "attributes-natural-language", NULL, language->language);
785 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
786 "requested-attributes",
787 (sizeof(jattrs) / sizeof(jattrs[0])),
788 NULL, jattrs);
790 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
791 "printer-uri", NULL, uri);
794 * Do the request and get back a response...
797 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
798 DEBUG(0,("Unable to get jobs for %s - %s\n", uri,
799 ippErrorString(cupsLastError())));
800 goto out;
803 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
804 DEBUG(0,("Unable to get jobs for %s - %s\n", uri,
805 ippErrorString(response->request.status.status_code)));
806 goto out;
810 * Process the jobs...
813 qcount = 0;
814 qalloc = 0;
815 queue = NULL;
817 for (attr = response->attrs; attr != NULL; attr = attr->next) {
819 * Skip leading attributes until we hit a job...
822 while (attr != NULL && attr->group_tag != IPP_TAG_JOB)
823 attr = attr->next;
825 if (attr == NULL)
826 break;
829 * Allocate memory as needed...
831 if (qcount >= qalloc) {
832 qalloc += 16;
834 queue = SMB_REALLOC_ARRAY(queue, print_queue_struct, qalloc);
836 if (queue == NULL) {
837 DEBUG(0,("cups_queue_get: Not enough memory!"));
838 qcount = 0;
839 goto out;
843 temp = queue + qcount;
844 memset(temp, 0, sizeof(print_queue_struct));
847 * Pull the needed attributes from this job...
850 job_id = 0;
851 job_priority = 50;
852 job_status = IPP_JOB_PENDING;
853 job_time = 0;
854 job_k_octets = 0;
855 user_name = NULL;
856 job_name = NULL;
858 while (attr != NULL && attr->group_tag == IPP_TAG_JOB) {
859 if (attr->name == NULL) {
860 attr = attr->next;
861 break;
864 if (strcmp(attr->name, "job-id") == 0 &&
865 attr->value_tag == IPP_TAG_INTEGER)
866 job_id = attr->values[0].integer;
868 if (strcmp(attr->name, "job-k-octets") == 0 &&
869 attr->value_tag == IPP_TAG_INTEGER)
870 job_k_octets = attr->values[0].integer;
872 if (strcmp(attr->name, "job-priority") == 0 &&
873 attr->value_tag == IPP_TAG_INTEGER)
874 job_priority = attr->values[0].integer;
876 if (strcmp(attr->name, "job-state") == 0 &&
877 attr->value_tag == IPP_TAG_ENUM)
878 job_status = (ipp_jstate_t)(attr->values[0].integer);
880 if (strcmp(attr->name, "time-at-creation") == 0 &&
881 attr->value_tag == IPP_TAG_INTEGER)
882 job_time = attr->values[0].integer;
884 if (strcmp(attr->name, "job-name") == 0 &&
885 attr->value_tag == IPP_TAG_NAME)
886 job_name = attr->values[0].string.text;
888 if (strcmp(attr->name, "job-originating-user-name") == 0 &&
889 attr->value_tag == IPP_TAG_NAME)
890 user_name = attr->values[0].string.text;
892 attr = attr->next;
896 * See if we have everything needed...
899 if (user_name == NULL || job_name == NULL || job_id == 0) {
900 if (attr == NULL)
901 break;
902 else
903 continue;
906 temp->job = job_id;
907 temp->size = job_k_octets * 1024;
908 temp->status = job_status == IPP_JOB_PENDING ? LPQ_QUEUED :
909 job_status == IPP_JOB_STOPPED ? LPQ_PAUSED :
910 job_status == IPP_JOB_HELD ? LPQ_PAUSED :
911 LPQ_PRINTING;
912 temp->priority = job_priority;
913 temp->time = job_time;
914 strncpy(temp->fs_user, user_name, sizeof(temp->fs_user) - 1);
915 strncpy(temp->fs_file, job_name, sizeof(temp->fs_file) - 1);
917 qcount ++;
919 if (attr == NULL)
920 break;
923 ippDelete(response);
924 response = NULL;
927 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the
928 * following attributes:
930 * attributes-charset
931 * attributes-natural-language
932 * requested-attributes
933 * printer-uri
936 request = ippNew();
938 request->request.op.operation_id = IPP_GET_PRINTER_ATTRIBUTES;
939 request->request.op.request_id = 1;
941 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
942 "attributes-charset", NULL, cupsLangEncoding(language));
944 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
945 "attributes-natural-language", NULL, language->language);
947 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
948 "requested-attributes",
949 (sizeof(pattrs) / sizeof(pattrs[0])),
950 NULL, pattrs);
952 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
953 "printer-uri", NULL, uri);
956 * Do the request and get back a response...
959 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
960 DEBUG(0,("Unable to get printer status for %s - %s\n", printername,
961 ippErrorString(cupsLastError())));
962 *q = queue;
963 goto out;
966 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
967 DEBUG(0,("Unable to get printer status for %s - %s\n", printername,
968 ippErrorString(response->request.status.status_code)));
969 *q = queue;
970 goto out;
974 * Get the current printer status and convert it to the SAMBA values.
977 if ((attr = ippFindAttribute(response, "printer-state", IPP_TAG_ENUM)) != NULL) {
978 if (attr->values[0].integer == IPP_PRINTER_STOPPED)
979 status->status = LPSTAT_STOPPED;
980 else
981 status->status = LPSTAT_OK;
984 if ((attr = ippFindAttribute(response, "printer-state-message",
985 IPP_TAG_TEXT)) != NULL)
986 fstrcpy(status->message, attr->values[0].string.text);
989 * Return the job queue...
992 *q = queue;
994 out:
995 if (response)
996 ippDelete(response);
998 if (language)
999 cupsLangFree(language);
1001 if (http)
1002 httpClose(http);
1004 return qcount;
1009 * 'cups_queue_pause()' - Pause a print queue.
1012 static int cups_queue_pause(int snum)
1014 int ret = 1; /* Return value */
1015 http_t *http = NULL; /* HTTP connection to server */
1016 ipp_t *request = NULL, /* IPP Request */
1017 *response = NULL; /* IPP Response */
1018 cups_lang_t *language = NULL; /* Default language */
1019 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
1022 DEBUG(5,("cups_queue_pause(%d)\n", snum));
1025 * Make sure we don't ask for passwords...
1028 cupsSetPasswordCB(cups_passwd_cb);
1031 * Try to connect to the server...
1034 if ((http = cups_connect()) == NULL) {
1035 goto out;
1039 * Build an IPP_PAUSE_PRINTER request, which requires the following
1040 * attributes:
1042 * attributes-charset
1043 * attributes-natural-language
1044 * printer-uri
1045 * requesting-user-name
1048 request = ippNew();
1050 request->request.op.operation_id = IPP_PAUSE_PRINTER;
1051 request->request.op.request_id = 1;
1053 language = cupsLangDefault();
1055 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1056 "attributes-charset", NULL, cupsLangEncoding(language));
1058 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1059 "attributes-natural-language", NULL, language->language);
1061 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s",
1062 PRINTERNAME(snum));
1064 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
1066 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
1067 NULL, current_user_info.unix_name);
1070 * Do the request and get back a response...
1073 if ((response = cupsDoRequest(http, request, "/admin/")) != NULL) {
1074 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1075 DEBUG(0,("Unable to pause printer %s - %s\n", PRINTERNAME(snum),
1076 ippErrorString(cupsLastError())));
1077 } else {
1078 ret = 0;
1080 } else {
1081 DEBUG(0,("Unable to pause printer %s - %s\n", PRINTERNAME(snum),
1082 ippErrorString(cupsLastError())));
1085 out:
1086 if (response)
1087 ippDelete(response);
1089 if (language)
1090 cupsLangFree(language);
1092 if (http)
1093 httpClose(http);
1095 return ret;
1100 * 'cups_queue_resume()' - Restart a print queue.
1103 static int cups_queue_resume(int snum)
1105 int ret = 1; /* Return value */
1106 http_t *http = NULL; /* HTTP connection to server */
1107 ipp_t *request = NULL, /* IPP Request */
1108 *response = NULL; /* IPP Response */
1109 cups_lang_t *language = NULL; /* Default language */
1110 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
1113 DEBUG(5,("cups_queue_resume(%d)\n", snum));
1116 * Make sure we don't ask for passwords...
1119 cupsSetPasswordCB(cups_passwd_cb);
1122 * Try to connect to the server...
1125 if ((http = cups_connect()) == NULL) {
1126 goto out;
1130 * Build an IPP_RESUME_PRINTER request, which requires the following
1131 * attributes:
1133 * attributes-charset
1134 * attributes-natural-language
1135 * printer-uri
1136 * requesting-user-name
1139 request = ippNew();
1141 request->request.op.operation_id = IPP_RESUME_PRINTER;
1142 request->request.op.request_id = 1;
1144 language = cupsLangDefault();
1146 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1147 "attributes-charset", NULL, cupsLangEncoding(language));
1149 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1150 "attributes-natural-language", NULL, language->language);
1152 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s",
1153 PRINTERNAME(snum));
1155 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
1157 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
1158 NULL, current_user_info.unix_name);
1161 * Do the request and get back a response...
1164 if ((response = cupsDoRequest(http, request, "/admin/")) != NULL) {
1165 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1166 DEBUG(0,("Unable to resume printer %s - %s\n", PRINTERNAME(snum),
1167 ippErrorString(cupsLastError())));
1168 } else {
1169 ret = 0;
1171 } else {
1172 DEBUG(0,("Unable to resume printer %s - %s\n", PRINTERNAME(snum),
1173 ippErrorString(cupsLastError())));
1176 out:
1177 if (response)
1178 ippDelete(response);
1180 if (language)
1181 cupsLangFree(language);
1183 if (http)
1184 httpClose(http);
1186 return ret;
1189 /*******************************************************************
1190 * CUPS printing interface definitions...
1191 ******************************************************************/
1193 struct printif cups_printif =
1195 PRINT_CUPS,
1196 cups_queue_get,
1197 cups_queue_pause,
1198 cups_queue_resume,
1199 cups_job_delete,
1200 cups_job_pause,
1201 cups_job_resume,
1202 cups_job_submit,
1205 bool cups_pull_comment_location(NT_PRINTER_INFO_LEVEL_2 *printer)
1207 http_t *http = NULL; /* HTTP connection to server */
1208 ipp_t *request = NULL, /* IPP Request */
1209 *response = NULL; /* IPP Response */
1210 ipp_attribute_t *attr; /* Current attribute */
1211 cups_lang_t *language = NULL; /* Default language */
1212 char *name, /* printer-name attribute */
1213 *info, /* printer-info attribute */
1214 *location; /* printer-location attribute */
1215 char uri[HTTP_MAX_URI];
1216 static const char *requested[] =/* Requested attributes */
1218 "printer-name",
1219 "printer-info",
1220 "printer-location"
1222 bool ret = False;
1224 DEBUG(5, ("pulling %s location\n", printer->sharename));
1227 * Make sure we don't ask for passwords...
1230 cupsSetPasswordCB(cups_passwd_cb);
1233 * Try to connect to the server...
1236 if ((http = cups_connect()) == NULL) {
1237 goto out;
1240 request = ippNew();
1242 request->request.op.operation_id = IPP_GET_PRINTER_ATTRIBUTES;
1243 request->request.op.request_id = 1;
1245 language = cupsLangDefault();
1247 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1248 "attributes-charset", NULL, cupsLangEncoding(language));
1250 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1251 "attributes-natural-language", NULL, language->language);
1253 slprintf(uri, sizeof(uri) - 1, "ipp://%s/printers/%s",
1254 lp_cups_server(), printer->sharename);
1256 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1257 "printer-uri", NULL, uri);
1259 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1260 "requested-attributes",
1261 (sizeof(requested) / sizeof(requested[0])),
1262 NULL, requested);
1265 * Do the request and get back a response...
1268 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
1269 DEBUG(0,("Unable to get printer attributes - %s\n",
1270 ippErrorString(cupsLastError())));
1271 goto out;
1274 for (attr = response->attrs; attr != NULL;) {
1276 * Skip leading attributes until we hit a printer...
1279 while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)
1280 attr = attr->next;
1282 if (attr == NULL)
1283 break;
1286 * Pull the needed attributes from this printer...
1289 name = NULL;
1290 info = NULL;
1291 location = NULL;
1293 while ( attr && (attr->group_tag == IPP_TAG_PRINTER) ) {
1294 /* Grab the comment if we don't have one */
1295 if ( (strcmp(attr->name, "printer-info") == 0)
1296 && (attr->value_tag == IPP_TAG_TEXT)
1297 && !strlen(printer->comment) )
1299 DEBUG(5,("cups_pull_comment_location: Using cups comment: %s\n",
1300 attr->values[0].string.text));
1301 strlcpy(printer->comment,
1302 attr->values[0].string.text,
1303 sizeof(printer->comment));
1306 /* Grab the location if we don't have one */
1307 if ( (strcmp(attr->name, "printer-location") == 0)
1308 && (attr->value_tag == IPP_TAG_TEXT)
1309 && !strlen(printer->location) )
1311 DEBUG(5,("cups_pull_comment_location: Using cups location: %s\n",
1312 attr->values[0].string.text));
1313 fstrcpy(printer->location,attr->values[0].string.text);
1316 attr = attr->next;
1320 * See if we have everything needed...
1323 if (name == NULL)
1324 break;
1328 ret = True;
1330 out:
1331 if (response)
1332 ippDelete(response);
1334 if (language)
1335 cupsLangFree(language);
1337 if (http)
1338 httpClose(http);
1340 return ret;
1343 #else
1344 /* this keeps fussy compilers happy */
1345 void print_cups_dummy(void);
1346 void print_cups_dummy(void) {}
1347 #endif /* HAVE_CUPS */