1/* 2 * Support code for the Common UNIX Printing System ("CUPS") 3 * 4 * Copyright 1999 by Easy Software Products 5 * 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 2 of the License, or 9 * (at your option) any later version. 10 * 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. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 19 */ 20 21#include "includes.h" 22#include "smb.h" 23 24#ifdef HAVE_LIBCUPS 25#include <cups/cups.h> 26#include <cups/language.h> 27 28extern int DEBUGLEVEL; 29 30 31/* 32 * 'cups_printer_fn()' - Call a function for every printer known to the 33 * system. 34 */ 35 36void cups_printer_fn(void (*fn)(char *, char *)) 37{ 38 http_t *http; /* HTTP connection to server */ 39 ipp_t *request, /* IPP Request */ 40 *response; /* IPP Response */ 41 ipp_attribute_t *attr; /* Current attribute */ 42 cups_lang_t *language; /* Default language */ 43 char *name, /* printer-name attribute */ 44 *make_model; /* make_model attribute */ 45 46 47 /* 48 * Try to connect to the server... 49 */ 50 51 if ((http = httpConnect(cupsServer(), ippPort())) == NULL) 52 return; 53 54 /* 55 * Build a CUPS_GET_PRINTERS request, which requires the following 56 * attributes: 57 * 58 * attributes-charset 59 * attributes-natural-language 60 */ 61 62 request = ippNew(); 63 64 request->request.op.operation_id = CUPS_GET_PRINTERS; 65 request->request.op.request_id = 1; 66 67 language = cupsLangDefault(); 68 69 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET, 70 "attributes-charset", NULL, cupsLangEncoding(language)); 71 72 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE, 73 "attributes-natural-language", NULL, language->language); 74 75 /* 76 * Do the request and get back a response... 77 */ 78 79 if ((response = cupsDoRequest(http, request, "/")) == NULL) 80 { 81 httpClose(http); 82 return; 83 } 84 85 for (attr = response->attrs; attr != NULL;) 86 { 87 /* 88 * Skip leading attributes until we hit a printer... 89 */ 90 91 while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER) 92 attr = attr->next; 93 94 if (attr == NULL) 95 break; 96 97 /* 98 * Pull the needed attributes from this printer... 99 */ 100 101 name = NULL; 102 make_model = NULL; 103 104 while (attr != NULL && attr->group_tag == IPP_TAG_PRINTER) 105 { 106 if (strcmp(attr->name, "printer-name") == 0 && 107 attr->value_tag == IPP_TAG_NAME) 108 name = attr->values[0].string.text; 109 110 if (strcmp(attr->name, "printer-make-and-model") == 0 && 111 attr->value_tag == IPP_TAG_TEXT) 112 make_model = attr->values[0].string.text; 113 114 attr = attr->next; 115 } 116 117 /* 118 * See if we have everything needed... 119 */ 120 121 if (name == NULL) 122 break; 123 124 (*fn)(name, make_model); 125 } 126 127 ippDelete(response); 128 httpClose(http); 129} 130 131 132/* 133 * provide the equivalent of pcap_printername_ok() for SVID/XPG4 conforming 134 * systems. 135 */ 136int cups_printername_ok(char *name) 137{ 138 http_t *http; /* HTTP connection to server */ 139 ipp_t *request, /* IPP Request */ 140 *response; /* IPP Response */ 141 ipp_attribute_t *attr; /* Current attribute */ 142 cups_lang_t *language; /* Default language */ 143 char uri[HTTP_MAX_URI]; /* printer-uri attribute */ 144 145 /* 146 * Try to connect to the server... 147 */ 148 149 if ((http = httpConnect(cupsServer(), ippPort())) == NULL) 150 return (0); 151 152 /* 153 * Build a IPP_GET_PRINTER_ATTRS request, which requires the following 154 * attributes: 155 * 156 * attributes-charset 157 * attributes-natural-language 158 * printer-uri 159 */ 160 161 request = ippNew(); 162 163 request->request.op.operation_id = IPP_GET_PRINTER_ATTRIBUTES; 164 request->request.op.request_id = 1; 165 166 language = cupsLangDefault(); 167 168 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET, 169 "attributes-charset", NULL, cupsLangEncoding(language)); 170 171 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE, 172 "attributes-natural-language", NULL, language->language); 173 174 snprintf(uri, sizeof(uri), "ipp://localhost/printers/%s", name); 175 176 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, 177 "printer-uri", NULL, uri); 178 179 /* 180 * Do the request and get back a response... 181 */ 182 183 if ((response = cupsDoRequest(http, request, "/")) == NULL) 184 { 185 httpClose(http); 186 return (0); 187 } 188 189 httpClose(http); 190 191 if (response->request.status.status_code >= IPP_OK_CONFLICT) 192 { 193 ippDelete(response); 194 return (0); 195 } 196 else 197 { 198 ippDelete(response); 199 return (1); 200 } 201} 202 203#else 204 /* this keeps fussy compilers happy */ 205 void print_cups_dummy(void) {} 206#endif /* HAVE_LIBCUPS */ 207