d8b716028e0d4ec265bb0a3f645c8938f6923594
[abartlet/samba.git/.git] / source3 / printing / pcap.c
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
42 struct pcap_cache {
43         char *name;
44         char *comment;
45         struct pcap_cache *next;
46 };
47
48 /* The systemwide printcap cache. */
49 static struct pcap_cache *pcap_cache = NULL;
50
51 bool pcap_cache_add_specific(struct pcap_cache **ppcache, const char *name, const char *comment)
52 {
53         struct pcap_cache *p;
54
55         if (name == NULL || ((p = SMB_MALLOC_P(struct pcap_cache)) == NULL))
56                 return false;
57
58         p->name = SMB_STRDUP(name);
59         p->comment = (comment && *comment) ? SMB_STRDUP(comment) : NULL;
60
61         DEBUG(11,("pcap_cache_add_specific: Adding name %s info %s\n",
62                 p->name, p->comment ? p->comment : ""));
63
64         p->next = *ppcache;
65         *ppcache = p;
66
67         return true;
68 }
69
70 void pcap_cache_destroy_specific(struct pcap_cache **pp_cache)
71 {
72         struct pcap_cache *p, *next;
73
74         for (p = *pp_cache; p != NULL; p = next) {
75                 next = p->next;
76
77                 SAFE_FREE(p->name);
78                 SAFE_FREE(p->comment);
79                 SAFE_FREE(p);
80         }
81         *pp_cache = NULL;
82 }
83
84 bool pcap_cache_add(const char *name, const char *comment)
85 {
86         return pcap_cache_add_specific(&pcap_cache, name, comment);
87 }
88
89 bool pcap_cache_loaded(void)
90 {
91         return (pcap_cache != NULL);
92 }
93
94 void pcap_cache_replace(const struct pcap_cache *pcache)
95 {
96         const struct pcap_cache *p;
97
98         pcap_cache_destroy_specific(&pcap_cache);
99         for (p = pcache; p; p = p->next) {
100                 pcap_cache_add(p->name, p->comment);
101         }
102 }
103
104 void pcap_cache_reload(void)
105 {
106         const char *pcap_name = lp_printcapname();
107         bool pcap_reloaded = False;
108         struct pcap_cache *tmp_cache = NULL;
109
110         DEBUG(3, ("reloading printcap cache\n"));
111
112         /* only go looking if no printcap name supplied */
113         if (pcap_name == NULL || *pcap_name == 0) {
114                 DEBUG(0, ("No printcap file name configured!\n"));
115                 return;
116         }
117
118         tmp_cache = pcap_cache;
119         pcap_cache = NULL;
120
121 #ifdef HAVE_CUPS
122         if (strequal(pcap_name, "cups")) {
123                 pcap_reloaded = cups_cache_reload(server_event_context(),
124                                                   server_messaging_context());
125                 goto done;
126         }
127 #endif
128
129 #ifdef HAVE_IPRINT
130         if (strequal(pcap_name, "iprint")) {
131                 pcap_reloaded = iprint_cache_reload();
132                 goto done;
133         }
134 #endif
135
136 #if defined(SYSV) || defined(HPUX)
137         if (strequal(pcap_name, "lpstat")) {
138                 pcap_reloaded = sysv_cache_reload();
139                 goto done;
140         }
141 #endif
142
143 #ifdef AIX
144         if (strstr_m(pcap_name, "/qconfig") != NULL) {
145                 pcap_reloaded = aix_cache_reload();
146                 goto done;
147         }
148 #endif
149
150         pcap_reloaded = std_pcap_cache_reload(pcap_name);
151
152 done:
153         DEBUG(3, ("reload status: %s\n", (pcap_reloaded) ? "ok" : "error"));
154
155         if (pcap_reloaded)
156                 pcap_cache_destroy_specific(&tmp_cache);
157         else {
158                 pcap_cache_destroy_specific(&pcap_cache);
159                 pcap_cache = tmp_cache;
160         }
161
162         return;
163 }
164
165
166 bool pcap_printername_ok(const char *printername)
167 {
168         struct pcap_cache *p;
169
170         for (p = pcap_cache; p != NULL; p = p->next)
171                 if (strequal(p->name, printername))
172                         return True;
173
174         return False;
175 }
176
177 /***************************************************************************
178 run a function on each printer name in the printcap file.
179 ***************************************************************************/
180
181 void pcap_printer_fn_specific(const struct pcap_cache *pc,
182                         void (*fn)(const char *, const char *, void *),
183                         void *pdata)
184 {
185         const struct pcap_cache *p;
186
187         for (p = pc; p != NULL; p = p->next)
188                 fn(p->name, p->comment, pdata);
189
190         return;
191 }
192
193 void pcap_printer_fn(void (*fn)(const char *, const char *, void *), void *pdata)
194 {
195         pcap_printer_fn_specific(pcap_cache, fn, pdata);
196 }