| 1 | /* | 
|---|
| 2 | Unix SMB/CIFS implementation. | 
|---|
| 3 | printcap parsing | 
|---|
| 4 | Copyright (C) Karl Auer 1993-1998 | 
|---|
| 5 |  | 
|---|
| 6 | Re-working by Martin Kiff, 1994 | 
|---|
| 7 |  | 
|---|
| 8 | Re-written again by Andrew Tridgell | 
|---|
| 9 |  | 
|---|
| 10 | Modified for SVID support by Norm Jacobs, 1997 | 
|---|
| 11 |  | 
|---|
| 12 | Modified for CUPS support by Michael Sweet, 1999 | 
|---|
| 13 |  | 
|---|
| 14 | This program is free software; you can redistribute it and/or modify | 
|---|
| 15 | it under the terms of the GNU General Public License as published by | 
|---|
| 16 | the Free Software Foundation; either version 3 of the License, or | 
|---|
| 17 | (at your option) any later version. | 
|---|
| 18 |  | 
|---|
| 19 | This program is distributed in the hope that it will be useful, | 
|---|
| 20 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 21 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 22 | GNU General Public License for more details. | 
|---|
| 23 |  | 
|---|
| 24 | You should have received a copy of the GNU General Public License | 
|---|
| 25 | along with this program.  If not, see <http://www.gnu.org/licenses/>. | 
|---|
| 26 | */ | 
|---|
| 27 |  | 
|---|
| 28 | /* | 
|---|
| 29 | *  Modified to call SVID/XPG4 support if printcap name is set to "lpstat" | 
|---|
| 30 | *  in smb.conf under Solaris. | 
|---|
| 31 | * | 
|---|
| 32 | *  Modified to call CUPS support if printcap name is set to "cups" | 
|---|
| 33 | *  in smb.conf. | 
|---|
| 34 | * | 
|---|
| 35 | *  Modified to call iPrint support if printcap name is set to "iprint" | 
|---|
| 36 | *  in smb.conf. | 
|---|
| 37 | */ | 
|---|
| 38 |  | 
|---|
| 39 | #include "includes.h" | 
|---|
| 40 | #include "printing/pcap.h" | 
|---|
| 41 | #include "printer_list.h" | 
|---|
| 42 |  | 
|---|
| 43 | struct pcap_cache { | 
|---|
| 44 | char *name; | 
|---|
| 45 | char *comment; | 
|---|
| 46 | char *location; | 
|---|
| 47 | struct pcap_cache *next; | 
|---|
| 48 | }; | 
|---|
| 49 |  | 
|---|
| 50 | bool pcap_cache_add_specific(struct pcap_cache **ppcache, const char *name, const char *comment, const char *location) | 
|---|
| 51 | { | 
|---|
| 52 | struct pcap_cache *p; | 
|---|
| 53 |  | 
|---|
| 54 | if (name == NULL || ((p = SMB_MALLOC_P(struct pcap_cache)) == NULL)) | 
|---|
| 55 | return false; | 
|---|
| 56 |  | 
|---|
| 57 | p->name = SMB_STRDUP(name); | 
|---|
| 58 | p->comment = (comment && *comment) ? SMB_STRDUP(comment) : NULL; | 
|---|
| 59 | p->location = (location && *location) ? SMB_STRDUP(location) : NULL; | 
|---|
| 60 |  | 
|---|
| 61 | DEBUG(11,("pcap_cache_add_specific: Adding name %s info %s, location: %s\n", | 
|---|
| 62 | p->name, p->comment ? p->comment : "", | 
|---|
| 63 | p->location ? p->location : "")); | 
|---|
| 64 |  | 
|---|
| 65 | p->next = *ppcache; | 
|---|
| 66 | *ppcache = p; | 
|---|
| 67 |  | 
|---|
| 68 | return true; | 
|---|
| 69 | } | 
|---|
| 70 |  | 
|---|
| 71 | void pcap_cache_destroy_specific(struct pcap_cache **pp_cache) | 
|---|
| 72 | { | 
|---|
| 73 | struct pcap_cache *p, *next; | 
|---|
| 74 |  | 
|---|
| 75 | for (p = *pp_cache; p != NULL; p = next) { | 
|---|
| 76 | next = p->next; | 
|---|
| 77 |  | 
|---|
| 78 | SAFE_FREE(p->name); | 
|---|
| 79 | SAFE_FREE(p->comment); | 
|---|
| 80 | SAFE_FREE(p->location); | 
|---|
| 81 | SAFE_FREE(p); | 
|---|
| 82 | } | 
|---|
| 83 | *pp_cache = NULL; | 
|---|
| 84 | } | 
|---|
| 85 |  | 
|---|
| 86 | bool pcap_cache_add(const char *name, const char *comment, const char *location) | 
|---|
| 87 | { | 
|---|
| 88 | NTSTATUS status; | 
|---|
| 89 | time_t t = time_mono(NULL); | 
|---|
| 90 |  | 
|---|
| 91 | status = printer_list_set_printer(talloc_tos(), name, comment, location, t); | 
|---|
| 92 | return NT_STATUS_IS_OK(status); | 
|---|
| 93 | } | 
|---|
| 94 |  | 
|---|
| 95 | bool pcap_cache_loaded(void) | 
|---|
| 96 | { | 
|---|
| 97 | NTSTATUS status; | 
|---|
| 98 | time_t last; | 
|---|
| 99 |  | 
|---|
| 100 | status = printer_list_get_last_refresh(&last); | 
|---|
| 101 | return NT_STATUS_IS_OK(status); | 
|---|
| 102 | } | 
|---|
| 103 |  | 
|---|
| 104 | bool pcap_cache_replace(const struct pcap_cache *pcache) | 
|---|
| 105 | { | 
|---|
| 106 | const struct pcap_cache *p; | 
|---|
| 107 | NTSTATUS status; | 
|---|
| 108 |  | 
|---|
| 109 | status = printer_list_mark_reload(); | 
|---|
| 110 | if (!NT_STATUS_IS_OK(status)) { | 
|---|
| 111 | DEBUG(0, ("Failed to mark printer list for reload!\n")); | 
|---|
| 112 | return false; | 
|---|
| 113 | } | 
|---|
| 114 |  | 
|---|
| 115 | for (p = pcache; p; p = p->next) { | 
|---|
| 116 | pcap_cache_add(p->name, p->comment, p->location); | 
|---|
| 117 | } | 
|---|
| 118 |  | 
|---|
| 119 | status = printer_list_clean_old(); | 
|---|
| 120 | if (!NT_STATUS_IS_OK(status)) { | 
|---|
| 121 | DEBUG(0, ("Failed to cleanup printer list!\n")); | 
|---|
| 122 | return false; | 
|---|
| 123 | } | 
|---|
| 124 |  | 
|---|
| 125 | return true; | 
|---|
| 126 | } | 
|---|
| 127 |  | 
|---|
| 128 | void pcap_cache_reload(struct tevent_context *ev, | 
|---|
| 129 | struct messaging_context *msg_ctx, | 
|---|
| 130 | void (*post_cache_fill_fn)(struct tevent_context *, | 
|---|
| 131 | struct messaging_context *)) | 
|---|
| 132 | { | 
|---|
| 133 | const char *pcap_name = lp_printcapname(); | 
|---|
| 134 | bool pcap_reloaded = False; | 
|---|
| 135 | NTSTATUS status; | 
|---|
| 136 | bool post_cache_fill_fn_handled = false; | 
|---|
| 137 |  | 
|---|
| 138 | DEBUG(3, ("reloading printcap cache\n")); | 
|---|
| 139 |  | 
|---|
| 140 | /* only go looking if no printcap name supplied */ | 
|---|
| 141 | if (pcap_name == NULL || *pcap_name == 0) { | 
|---|
| 142 | DEBUG(0, ("No printcap file name configured!\n")); | 
|---|
| 143 | return; | 
|---|
| 144 | } | 
|---|
| 145 |  | 
|---|
| 146 | status = printer_list_mark_reload(); | 
|---|
| 147 | if (!NT_STATUS_IS_OK(status)) { | 
|---|
| 148 | DEBUG(0, ("Failed to mark printer list for reload!\n")); | 
|---|
| 149 | return; | 
|---|
| 150 | } | 
|---|
| 151 |  | 
|---|
| 152 | #ifdef HAVE_CUPS | 
|---|
| 153 | if (strequal(pcap_name, "cups")) { | 
|---|
| 154 | pcap_reloaded = cups_cache_reload(ev, msg_ctx, | 
|---|
| 155 | post_cache_fill_fn); | 
|---|
| 156 | /* | 
|---|
| 157 | * cups_cache_reload() is async and calls post_cache_fill_fn() | 
|---|
| 158 | * on successful completion | 
|---|
| 159 | */ | 
|---|
| 160 | post_cache_fill_fn_handled = true; | 
|---|
| 161 | goto done; | 
|---|
| 162 | } | 
|---|
| 163 | #endif | 
|---|
| 164 |  | 
|---|
| 165 | #ifdef HAVE_IPRINT | 
|---|
| 166 | if (strequal(pcap_name, "iprint")) { | 
|---|
| 167 | pcap_reloaded = iprint_cache_reload(); | 
|---|
| 168 | goto done; | 
|---|
| 169 | } | 
|---|
| 170 | #endif | 
|---|
| 171 |  | 
|---|
| 172 | #if defined(SYSV) || defined(HPUX) | 
|---|
| 173 | if (strequal(pcap_name, "lpstat")) { | 
|---|
| 174 | pcap_reloaded = sysv_cache_reload(); | 
|---|
| 175 | goto done; | 
|---|
| 176 | } | 
|---|
| 177 | #endif | 
|---|
| 178 |  | 
|---|
| 179 | #ifdef AIX | 
|---|
| 180 | if (strstr_m(pcap_name, "/qconfig") != NULL) { | 
|---|
| 181 | pcap_reloaded = aix_cache_reload(); | 
|---|
| 182 | goto done; | 
|---|
| 183 | } | 
|---|
| 184 | #endif | 
|---|
| 185 |  | 
|---|
| 186 | pcap_reloaded = std_pcap_cache_reload(pcap_name); | 
|---|
| 187 |  | 
|---|
| 188 | done: | 
|---|
| 189 | DEBUG(3, ("reload status: %s\n", (pcap_reloaded) ? "ok" : "error")); | 
|---|
| 190 |  | 
|---|
| 191 | if ((pcap_reloaded) && (post_cache_fill_fn_handled == false)) { | 
|---|
| 192 | /* cleanup old entries only if the operation was successful, | 
|---|
| 193 | * otherwise keep around the old entries until we can | 
|---|
| 194 | * successfuly reaload */ | 
|---|
| 195 | status = printer_list_clean_old(); | 
|---|
| 196 | if (!NT_STATUS_IS_OK(status)) { | 
|---|
| 197 | DEBUG(0, ("Failed to cleanup printer list!\n")); | 
|---|
| 198 | } | 
|---|
| 199 | if (post_cache_fill_fn != NULL) { | 
|---|
| 200 | post_cache_fill_fn(ev, msg_ctx); | 
|---|
| 201 | } | 
|---|
| 202 | } | 
|---|
| 203 |  | 
|---|
| 204 | return; | 
|---|
| 205 | } | 
|---|
| 206 |  | 
|---|
| 207 |  | 
|---|
| 208 | bool pcap_printername_ok(const char *printername) | 
|---|
| 209 | { | 
|---|
| 210 | NTSTATUS status; | 
|---|
| 211 |  | 
|---|
| 212 | status = printer_list_get_printer(talloc_tos(), printername, NULL, NULL, 0); | 
|---|
| 213 | return NT_STATUS_IS_OK(status); | 
|---|
| 214 | } | 
|---|
| 215 |  | 
|---|
| 216 | /*************************************************************************** | 
|---|
| 217 | run a function on each printer name in the printcap file. | 
|---|
| 218 | ***************************************************************************/ | 
|---|
| 219 |  | 
|---|
| 220 | void pcap_printer_fn_specific(const struct pcap_cache *pc, | 
|---|
| 221 | void (*fn)(const char *, const char *, const char *, void *), | 
|---|
| 222 | void *pdata) | 
|---|
| 223 | { | 
|---|
| 224 | const struct pcap_cache *p; | 
|---|
| 225 |  | 
|---|
| 226 | for (p = pc; p != NULL; p = p->next) | 
|---|
| 227 | fn(p->name, p->comment, p->location, pdata); | 
|---|
| 228 |  | 
|---|
| 229 | return; | 
|---|
| 230 | } | 
|---|
| 231 |  | 
|---|
| 232 | void pcap_printer_fn(void (*fn)(const char *, const char *, const char *, void *), void *pdata) | 
|---|
| 233 | { | 
|---|
| 234 | NTSTATUS status; | 
|---|
| 235 |  | 
|---|
| 236 | status = printer_list_run_fn(fn, pdata); | 
|---|
| 237 | if (!NT_STATUS_IS_OK(status)) { | 
|---|
| 238 | DEBUG(3, ("Failed to run fn for all printers!\n")); | 
|---|
| 239 | } | 
|---|
| 240 | return; | 
|---|
| 241 | } | 
|---|