6392755cf5ae15eb7011648fb0697c7824bc527e
[kamenim/samba.git] / source3 / printing / printer_list.c
1 /*
2    Unix SMB/CIFS implementation.
3    Share Database of available printers.
4    Copyright (C) Simo Sorce 2010
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 3 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, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "dbwrap.h"
22 #include "printer_list.h"
23
24 #define PL_DB_NAME() lock_path("printer_list.tdb")
25 #define PL_KEY_PREFIX "PRINTERLIST/PRN/"
26 #define PL_KEY_FORMAT PL_KEY_PREFIX"%s"
27 #define PL_TIMESTAMP_KEY "PRINTERLIST/GLOBAL/LAST_REFRESH"
28 #define PL_DATA_FORMAT "ddPP"
29 #define PL_TSTAMP_FORMAT "dd"
30
31 static struct db_context *get_printer_list_db(void)
32 {
33         static struct db_context *db;
34
35         if (db != NULL) {
36                 return db;
37         }
38         db = db_open(talloc_autofree_context(), PL_DB_NAME(), 0,
39                      TDB_DEFAULT|TDB_CLEAR_IF_FIRST,
40                      O_RDWR|O_CREAT, 0644);
41         return db;
42 }
43
44 bool printer_list_parent_init(void)
45 {
46         struct db_context *db;
47
48         /*
49          * Open the tdb in the parent process (smbd) so that our
50          * CLEAR_IF_FIRST optimization in tdb_reopen_all can properly
51          * work.
52          */
53
54         db = get_printer_list_db();
55         if (db == NULL) {
56                 DEBUG(1, ("could not open Printer List Database: %s\n",
57                           strerror(errno)));
58                 return false;
59         }
60         return true;
61 }
62
63 NTSTATUS printer_list_get_printer(TALLOC_CTX *mem_ctx,
64                                   const char *name,
65                                   const char **comment,
66                                   time_t *last_refresh)
67 {
68         struct db_context *db;
69         char *key;
70         TDB_DATA data;
71         uint32_t time_h, time_l;
72         char *nstr = NULL;
73         char *cstr = NULL;
74         NTSTATUS status;
75         int ret;
76
77         db = get_printer_list_db();
78         if (db == NULL) {
79                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
80         }
81
82         key = talloc_asprintf(mem_ctx, PL_KEY_FORMAT, name);
83         if (!key) {
84                 DEBUG(0, ("Failed to allocate key name!\n"));
85                 return NT_STATUS_NO_MEMORY;
86         }
87
88         data = dbwrap_fetch_bystring_upper(db, key, key);
89         if (data.dptr == NULL) {
90                 DEBUG(1, ("Failed to fetch record!\n"));
91                 status = NT_STATUS_NOT_FOUND;
92                 goto done;
93         }
94
95         ret = tdb_unpack(data.dptr, data.dsize,
96                          PL_DATA_FORMAT,
97                          &time_h, &time_l, &nstr, &cstr);
98         if (ret == -1) {
99                 DEBUG(1, ("Failed to un pack printer data"));
100                 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
101                 goto done;
102         }
103
104         if (last_refresh) {
105                 *last_refresh = (time_t)(((uint64_t)time_h << 32) + time_l);
106         }
107
108         if (comment) {
109                 *comment = talloc_strdup(mem_ctx, cstr);
110                 if (!*comment) {
111                         DEBUG(1, ("Failed to strdup comment!\n"));
112                         status = NT_STATUS_NO_MEMORY;
113                         goto done;
114                 }
115         }
116
117         status = NT_STATUS_OK;
118
119 done:
120         SAFE_FREE(nstr);
121         SAFE_FREE(cstr);
122         TALLOC_FREE(key);
123         return status;
124 }
125
126 NTSTATUS printer_list_set_printer(TALLOC_CTX *mem_ctx,
127                                   const char *name,
128                                   const char *comment,
129                                   time_t last_refresh)
130 {
131         struct db_context *db;
132         char *key;
133         TDB_DATA data;
134         uint64_t time_64;
135         uint32_t time_h, time_l;
136         const char *str = NULL;
137         NTSTATUS status;
138         int len;
139
140         db = get_printer_list_db();
141         if (db == NULL) {
142                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
143         }
144
145         key = talloc_asprintf(mem_ctx, PL_KEY_FORMAT, name);
146         if (!key) {
147                 DEBUG(0, ("Failed to allocate key name!\n"));
148                 return NT_STATUS_NO_MEMORY;
149         }
150
151         if (comment) {
152                 str = comment;
153         } else {
154                 str = "";
155         }
156
157         time_64 = last_refresh;
158         time_l = time_64 & 0xFFFFFFFFL;
159         time_h = time_64 >> 32;
160
161         len = tdb_pack(NULL, 0, PL_DATA_FORMAT, time_h, time_l, name, str);
162
163         data.dptr = talloc_array(key, uint8_t, len);
164         if (!data.dptr) {
165                 DEBUG(0, ("Failed to allocate tdb data buffer!\n"));
166                 status = NT_STATUS_NO_MEMORY;
167                 goto done;
168         }
169         data.dsize = len;
170
171         len = tdb_pack(data.dptr, data.dsize,
172                        PL_DATA_FORMAT, time_h, time_l, name, str);
173
174         status = dbwrap_store_bystring_upper(db, key, data, TDB_REPLACE);
175
176 done:
177         TALLOC_FREE(key);
178         return status;
179 }
180
181 NTSTATUS printer_list_get_last_refresh(time_t *last_refresh)
182 {
183         struct db_context *db;
184         TDB_DATA data;
185         uint32_t time_h, time_l;
186         NTSTATUS status;
187         int ret;
188
189         db = get_printer_list_db();
190         if (db == NULL) {
191                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
192         }
193
194         ZERO_STRUCT(data);
195
196         data = dbwrap_fetch_bystring(db, talloc_tos(), PL_TIMESTAMP_KEY);
197         if (data.dptr == NULL) {
198                 DEBUG(1, ("Failed to fetch record!\n"));
199                 status = NT_STATUS_NOT_FOUND;
200                 goto done;
201         }
202
203         ret = tdb_unpack(data.dptr, data.dsize,
204                          PL_TSTAMP_FORMAT, &time_h, &time_l);
205         if (ret == -1) {
206                 DEBUG(1, ("Failed to un pack printer data"));
207                 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
208                 goto done;
209         }
210
211         *last_refresh = (time_t)(((uint64_t)time_h << 32) + time_l);
212         status = NT_STATUS_OK;
213
214 done:
215         return status;
216 }
217
218 bool printer_list_need_refresh(void)
219 {
220         NTSTATUS status;
221         time_t now = time(NULL);
222         time_t last_refresh;
223
224         status = printer_list_get_last_refresh(&last_refresh);
225         if (!NT_STATUS_IS_OK(status)) {
226                 return true;
227         }
228
229         if (now > last_refresh) {
230                 /* if refresh occurred last than 1 seconds ago,
231                  * then we probably don't need to refresh */
232                 if ((now - last_refresh) < 1) {
233                         return false;
234                 }
235         } else {
236                 /* last_refresh newer than now, wow, someone just updated the
237                  * cache under our nose, do not do again. */
238                 return false;
239         }
240
241         return true;
242 }
243
244 NTSTATUS printer_list_mark_reload(void)
245 {
246         struct db_context *db;
247         TDB_DATA data;
248         uint32_t time_h, time_l;
249         time_t now = time(NULL);
250         NTSTATUS status;
251         int len;
252
253         db = get_printer_list_db();
254         if (db == NULL) {
255                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
256         }
257
258         time_l = ((uint64_t)now) & 0xFFFFFFFFL;
259         time_h = ((uint64_t)now) >> 32;
260
261         len = tdb_pack(NULL, 0, PL_TSTAMP_FORMAT, time_h, time_l);
262
263         data.dptr = talloc_array(talloc_tos(), uint8_t, len);
264         if (!data.dptr) {
265                 DEBUG(0, ("Failed to allocate tdb data buffer!\n"));
266                 status = NT_STATUS_NO_MEMORY;
267                 goto done;
268         }
269         data.dsize = len;
270
271         len = tdb_pack(data.dptr, data.dsize,
272                        PL_TSTAMP_FORMAT, time_h, time_l);
273
274         status = dbwrap_store_bystring(db, PL_TIMESTAMP_KEY,
275                                                 data, TDB_REPLACE);
276
277 done:
278         TALLOC_FREE(data.dptr);
279         return status;
280 }
281
282 typedef int (printer_list_trv_fn_t)(struct db_record *, void *);
283
284 static NTSTATUS printer_list_traverse(printer_list_trv_fn_t *fn,
285                                                 void *private_data)
286 {
287         struct db_context *db;
288         int ret;
289
290         db = get_printer_list_db();
291         if (db == NULL) {
292                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
293         }
294
295         ret = db->traverse(db, fn, private_data);
296         if (ret != 0) {
297                 return NT_STATUS_UNSUCCESSFUL;
298         }
299
300         return NT_STATUS_OK;
301 }
302
303 struct printer_list_clean_state {
304         time_t last_refresh;
305         NTSTATUS status;
306 };
307
308 static int printer_list_clean_fn(struct db_record *rec, void *private_data)
309 {
310         struct printer_list_clean_state *state =
311                         (struct printer_list_clean_state *)private_data;
312         uint32_t time_h, time_l;
313         time_t refresh;
314         char *name;
315         char *comment;
316         int ret;
317
318         /* skip anything that does not contain PL_DATA_FORMAT data */
319         if (strncmp((char *)rec->key.dptr,
320                     PL_KEY_PREFIX, sizeof(PL_KEY_PREFIX)-1)) {
321                 return 0;
322         }
323
324         ret = tdb_unpack(rec->value.dptr, rec->value.dsize,
325                          PL_DATA_FORMAT, &time_h, &time_l, &name, &comment);
326         if (ret == -1) {
327                 DEBUG(1, ("Failed to un pack printer data"));
328                 state->status = NT_STATUS_INTERNAL_DB_CORRUPTION;
329                 return -1;
330         }
331
332         SAFE_FREE(name);
333         SAFE_FREE(comment);
334
335         refresh = (time_t)(((uint64_t)time_h << 32) + time_l);
336
337         if (refresh < state->last_refresh) {
338                 state->status = rec->delete_rec(rec);
339                 if (!NT_STATUS_IS_OK(state->status)) {
340                         return -1;
341                 }
342         }
343
344         return 0;
345 }
346
347 NTSTATUS printer_list_clean_old(void)
348 {
349         struct printer_list_clean_state state;
350         NTSTATUS status;
351
352         status = printer_list_get_last_refresh(&state.last_refresh);
353         if (!NT_STATUS_IS_OK(status)) {
354                 return status;
355         }
356
357         state.status = NT_STATUS_OK;
358
359         status = printer_list_traverse(printer_list_clean_fn, &state);
360         if (NT_STATUS_EQUAL(status, NT_STATUS_UNSUCCESSFUL) &&
361             !NT_STATUS_IS_OK(state.status)) {
362                 status = state.status;
363         }
364
365         return status;
366 }
367
368 struct printer_list_exec_state {
369         void (*fn)(const char *, const char *, void *);
370         void *private_data;
371         NTSTATUS status;
372 };
373
374 static int printer_list_exec_fn(struct db_record *rec, void *private_data)
375 {
376         struct printer_list_exec_state *state =
377                         (struct printer_list_exec_state *)private_data;
378         uint32_t time_h, time_l;
379         char *name;
380         char *comment;
381         int ret;
382
383         ret = tdb_unpack(rec->value.dptr, rec->value.dsize,
384                          PL_DATA_FORMAT, &time_h, &time_l, &name, &comment);
385         if (ret == -1) {
386                 DEBUG(1, ("Failed to un pack printer data"));
387                 state->status = NT_STATUS_INTERNAL_DB_CORRUPTION;
388                 return -1;
389         }
390
391         state->fn(name, comment, state->private_data);
392
393         SAFE_FREE(name);
394         SAFE_FREE(comment);
395         return 0;
396 }
397
398 NTSTATUS printer_list_run_fn(void (*fn)(const char *, const char *, void *),
399                              void *private_data)
400 {
401         struct printer_list_exec_state state;
402         NTSTATUS status;
403
404         state.fn = fn;
405         state.private_data = private_data;
406         state.status = NT_STATUS_OK;
407
408         status = printer_list_traverse(printer_list_exec_fn, &state);
409         if (NT_STATUS_EQUAL(status, NT_STATUS_UNSUCCESSFUL) &&
410             !NT_STATUS_IS_OK(state.status)) {
411                 status = state.status;
412         }
413
414         return status;
415 }