printing: check lp_load_printers() prior to pcap cache update
[Samba.git] / source3 / printing / pcap.c
blobd771cd9dfeb9e11178a47db736d336cdc9d6c393
1 /*
2 Unix SMB/CIFS implementation.
3 printcap parsing
4 Copyright (C) Karl Auer 1993-1998
6 Re-working by Martin Kiff, 1994
8 Re-written again by Andrew Tridgell
10 Modified for SVID support by Norm Jacobs, 1997
12 Modified for CUPS support by Michael Sweet, 1999
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.
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.
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/>.
29 * Modified to call SVID/XPG4 support if printcap name is set to "lpstat"
30 * in smb.conf under Solaris.
32 * Modified to call CUPS support if printcap name is set to "cups"
33 * in smb.conf.
35 * Modified to call iPrint support if printcap name is set to "iprint"
36 * in smb.conf.
39 #include "includes.h"
40 #include "printing/pcap.h"
41 #include "printer_list.h"
43 struct pcap_cache {
44 char *name;
45 char *comment;
46 char *location;
47 struct pcap_cache *next;
50 bool pcap_cache_add_specific(struct pcap_cache **ppcache, const char *name, const char *comment, const char *location)
52 struct pcap_cache *p;
54 if (name == NULL || ((p = SMB_MALLOC_P(struct pcap_cache)) == NULL))
55 return false;
57 p->name = SMB_STRDUP(name);
58 p->comment = (comment && *comment) ? SMB_STRDUP(comment) : NULL;
59 p->location = (location && *location) ? SMB_STRDUP(location) : NULL;
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 : ""));
65 p->next = *ppcache;
66 *ppcache = p;
68 return true;
71 void pcap_cache_destroy_specific(struct pcap_cache **pp_cache)
73 struct pcap_cache *p, *next;
75 for (p = *pp_cache; p != NULL; p = next) {
76 next = p->next;
78 SAFE_FREE(p->name);
79 SAFE_FREE(p->comment);
80 SAFE_FREE(p->location);
81 SAFE_FREE(p);
83 *pp_cache = NULL;
86 bool pcap_cache_loaded(time_t *_last_change)
88 NTSTATUS status;
89 time_t last;
91 status = printer_list_get_last_refresh(&last);
92 if (!NT_STATUS_IS_OK(status)) {
93 return false;
95 if (_last_change != NULL) {
96 *_last_change = last;
98 return true;
101 bool pcap_cache_replace(const struct pcap_cache *pcache)
103 const struct pcap_cache *p;
104 NTSTATUS status;
105 time_t t = time_mono(NULL);
107 status = printer_list_mark_reload();
108 if (!NT_STATUS_IS_OK(status)) {
109 DEBUG(0, ("Failed to mark printer list for reload!\n"));
110 return false;
113 for (p = pcache; p; p = p->next) {
114 status = printer_list_set_printer(talloc_tos(), p->name,
115 p->comment, p->location, t);
116 if (!NT_STATUS_IS_OK(status)) {
117 return false;
121 status = printer_list_clean_old();
122 if (!NT_STATUS_IS_OK(status)) {
123 DEBUG(0, ("Failed to cleanup printer list!\n"));
124 return false;
127 return true;
130 void pcap_cache_reload(struct tevent_context *ev,
131 struct messaging_context *msg_ctx,
132 void (*post_cache_fill_fn)(struct tevent_context *,
133 struct messaging_context *))
135 const char *pcap_name = lp_printcapname();
136 bool pcap_reloaded = False;
137 bool post_cache_fill_fn_handled = false;
138 struct pcap_cache *pcache = NULL;
140 DEBUG(3, ("reloading printcap cache\n"));
142 if (!lp_load_printers()) {
143 DBG_NOTICE("skipping reload - load printers disabled\n");
144 return;
147 /* only go looking if no printcap name supplied */
148 if (pcap_name == NULL || *pcap_name == 0) {
149 DEBUG(0, ("No printcap file name configured!\n"));
150 return;
153 #ifdef HAVE_CUPS
154 if (strequal(pcap_name, "cups")) {
155 pcap_reloaded = cups_cache_reload(ev, msg_ctx,
156 post_cache_fill_fn);
158 * cups_cache_reload() is async and calls post_cache_fill_fn()
159 * on successful completion
161 post_cache_fill_fn_handled = true;
162 goto done;
164 #endif
166 #ifdef HAVE_IPRINT
167 if (strequal(pcap_name, "iprint")) {
168 pcap_reloaded = iprint_cache_reload(&pcache);
169 goto done;
171 #endif
173 #if defined(SYSV) || defined(HPUX)
174 if (strequal(pcap_name, "lpstat")) {
175 pcap_reloaded = sysv_cache_reload(&pcache);
176 goto done;
178 #endif
180 #ifdef AIX
181 if (strstr_m(pcap_name, "/qconfig") != NULL) {
182 pcap_reloaded = aix_cache_reload(&pcache);
183 goto done;
185 #endif
187 pcap_reloaded = std_pcap_cache_reload(pcap_name, &pcache);
189 /* Fix silly compiler warning about done not being used if none of the above
190 * ifdefs are used */
191 #if defined(HAVE_CUPS) || defined(HAVE_IPRINT) || defined(SYSV) || defined(HPUX) || defined(AIX)
192 done:
193 #endif
194 DEBUG(3, ("reload status: %s\n", (pcap_reloaded) ? "ok" : "error"));
196 if ((pcap_reloaded) && (post_cache_fill_fn_handled == false)) {
197 /* cleanup old entries only if the operation was successful,
198 * otherwise keep around the old entries until we can
199 * successfully reload */
201 if (!pcap_cache_replace(pcache)) {
202 DEBUG(0, ("Failed to replace printer list!\n"));
205 if (post_cache_fill_fn != NULL) {
206 post_cache_fill_fn(ev, msg_ctx);
209 pcap_cache_destroy_specific(&pcache);
211 return;
215 bool pcap_printername_ok(const char *printername)
217 NTSTATUS status;
219 status = printer_list_get_printer(talloc_tos(), printername, NULL, NULL, 0);
220 return NT_STATUS_IS_OK(status);
223 /***************************************************************************
224 run a function on each printer name in the printcap file.
225 ***************************************************************************/
227 void pcap_printer_fn_specific(const struct pcap_cache *pc,
228 void (*fn)(const char *, const char *, const char *, void *),
229 void *pdata)
231 const struct pcap_cache *p;
233 for (p = pc; p != NULL; p = p->next)
234 fn(p->name, p->comment, p->location, pdata);
236 return;
239 void pcap_printer_read_fn(void (*fn)(const char *, const char *, const char *, void *), void *pdata)
241 NTSTATUS status;
243 status = printer_list_read_run_fn(fn, pdata);
244 if (!NT_STATUS_IS_OK(status)) {
245 DEBUG(3, ("Failed to run fn for all printers!\n"));
247 return;