2e544b7cdc373528e0ac5cedf6125795c704eb2a
[kamenim/samba.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 #include "printer_list.h"
42
43 struct pcap_cache {
44         char *name;
45         char *comment;
46         struct pcap_cache *next;
47 };
48
49 bool pcap_cache_add_specific(struct pcap_cache **ppcache, const char *name, const char *comment)
50 {
51         struct pcap_cache *p;
52
53         if (name == NULL || ((p = SMB_MALLOC_P(struct pcap_cache)) == NULL))
54                 return false;
55
56         p->name = SMB_STRDUP(name);
57         p->comment = (comment && *comment) ? SMB_STRDUP(comment) : NULL;
58
59         DEBUG(11,("pcap_cache_add_specific: Adding name %s info %s\n",
60                 p->name, p->comment ? p->comment : ""));
61
62         p->next = *ppcache;
63         *ppcache = p;
64
65         return true;
66 }
67
68 void pcap_cache_destroy_specific(struct pcap_cache **pp_cache)
69 {
70         struct pcap_cache *p, *next;
71
72         for (p = *pp_cache; p != NULL; p = next) {
73                 next = p->next;
74
75                 SAFE_FREE(p->name);
76                 SAFE_FREE(p->comment);
77                 SAFE_FREE(p);
78         }
79         *pp_cache = NULL;
80 }
81
82 bool pcap_cache_add(const char *name, const char *comment)
83 {
84         NTSTATUS status;
85         time_t t = time(NULL);
86
87         status = printer_list_set_printer(talloc_tos(), name, comment, t);
88         return NT_STATUS_IS_OK(status);
89 }
90
91 bool pcap_cache_loaded(void)
92 {
93         NTSTATUS status;
94         time_t last;
95
96         status = printer_list_get_last_refresh(&last);
97         return NT_STATUS_IS_OK(status);
98 }
99
100 void pcap_cache_replace(const struct pcap_cache *pcache)
101 {
102         const struct pcap_cache *p;
103
104         for (p = pcache; p; p = p->next) {
105                 pcap_cache_add(p->name, p->comment);
106         }
107 }
108
109 void pcap_cache_reload(struct tevent_context *ev,
110                        struct messaging_context *msg_ctx)
111 {
112         const char *pcap_name = lp_printcapname();
113         bool pcap_reloaded = False;
114         NTSTATUS status;
115
116         DEBUG(3, ("reloading printcap cache\n"));
117
118         /* only go looking if no printcap name supplied */
119         if (pcap_name == NULL || *pcap_name == 0) {
120                 DEBUG(0, ("No printcap file name configured!\n"));
121                 return;
122         }
123
124         status = printer_list_mark_reload();
125         if (!NT_STATUS_IS_OK(status)) {
126                 DEBUG(0, ("Failed to mark printer list for reload!\n"));
127                 return;
128         }
129
130 #ifdef HAVE_CUPS
131         if (strequal(pcap_name, "cups")) {
132                 pcap_reloaded = cups_cache_reload(ev, msg_ctx);
133                 goto done;
134         }
135 #endif
136
137 #ifdef HAVE_IPRINT
138         if (strequal(pcap_name, "iprint")) {
139                 pcap_reloaded = iprint_cache_reload();
140                 goto done;
141         }
142 #endif
143
144 #if defined(SYSV) || defined(HPUX)
145         if (strequal(pcap_name, "lpstat")) {
146                 pcap_reloaded = sysv_cache_reload();
147                 goto done;
148         }
149 #endif
150
151 #ifdef AIX
152         if (strstr_m(pcap_name, "/qconfig") != NULL) {
153                 pcap_reloaded = aix_cache_reload();
154                 goto done;
155         }
156 #endif
157
158         pcap_reloaded = std_pcap_cache_reload(pcap_name);
159
160 done:
161         DEBUG(3, ("reload status: %s\n", (pcap_reloaded) ? "ok" : "error"));
162
163         if (pcap_reloaded) {
164                 /* cleanup old entries only if the operation was successful,
165                  * otherwise keep around the old entries until we can
166                  * successfuly reaload */
167                 status = printer_list_clean_old();
168                 if (!NT_STATUS_IS_OK(status)) {
169                         DEBUG(0, ("Failed to cleanup printer list!\n"));
170                 }
171         }
172
173         return;
174 }
175
176
177 bool pcap_printername_ok(const char *printername)
178 {
179         NTSTATUS status;
180
181         status = printer_list_get_printer(talloc_tos(), printername, NULL, 0);
182         return NT_STATUS_IS_OK(status);
183 }
184
185 /***************************************************************************
186 run a function on each printer name in the printcap file.
187 ***************************************************************************/
188
189 void pcap_printer_fn_specific(const struct pcap_cache *pc,
190                         void (*fn)(const char *, const char *, void *),
191                         void *pdata)
192 {
193         const struct pcap_cache *p;
194
195         for (p = pc; p != NULL; p = p->next)
196                 fn(p->name, p->comment, pdata);
197
198         return;
199 }
200
201 void pcap_printer_fn(void (*fn)(const char *, const char *, void *), void *pdata)
202 {
203         NTSTATUS status;
204
205         status = printer_list_run_fn(fn, pdata);
206         if (!NT_STATUS_IS_OK(status)) {
207                 DEBUG(3, ("Failed to run fn for all printers!\n"));
208         }
209         return;
210 }