2 Unix SMB/CIFS implementation.
3 Share Database of available printers.
4 Copyright (C) Simo Sorce 2010
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.
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.
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/>.
21 #include "system/filesys.h"
23 #include "printer_list.h"
25 #define PL_DB_NAME() lock_path("printer_list.tdb")
26 #define PL_KEY_PREFIX "PRINTERLIST/PRN/"
27 #define PL_KEY_FORMAT PL_KEY_PREFIX"%s"
28 #define PL_TIMESTAMP_KEY "PRINTERLIST/GLOBAL/LAST_REFRESH"
29 #define PL_DATA_FORMAT "ddPP"
30 #define PL_TSTAMP_FORMAT "dd"
32 static struct db_context *get_printer_list_db(void)
34 static struct db_context *db;
39 db = db_open(NULL, PL_DB_NAME(), 0,
40 TDB_DEFAULT|TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH,
41 O_RDWR|O_CREAT, 0644);
45 bool printer_list_parent_init(void)
47 struct db_context *db;
50 * Open the tdb in the parent process (smbd) so that our
51 * CLEAR_IF_FIRST optimization in tdb_reopen_all can properly
55 db = get_printer_list_db();
57 DEBUG(1, ("could not open Printer List Database: %s\n",
64 NTSTATUS printer_list_get_printer(TALLOC_CTX *mem_ctx,
69 struct db_context *db;
72 uint32_t time_h, time_l;
78 db = get_printer_list_db();
80 return NT_STATUS_INTERNAL_DB_CORRUPTION;
83 key = talloc_asprintf(mem_ctx, PL_KEY_FORMAT, name);
85 DEBUG(0, ("Failed to allocate key name!\n"));
86 return NT_STATUS_NO_MEMORY;
89 data = dbwrap_fetch_bystring_upper(db, key, key);
90 if (data.dptr == NULL) {
91 DEBUG(1, ("Failed to fetch record!\n"));
92 status = NT_STATUS_NOT_FOUND;
96 ret = tdb_unpack(data.dptr, data.dsize,
98 &time_h, &time_l, &nstr, &cstr);
100 DEBUG(1, ("Failed to un pack printer data"));
101 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
106 *last_refresh = (time_t)(((uint64_t)time_h << 32) + time_l);
110 *comment = talloc_strdup(mem_ctx, cstr);
112 DEBUG(1, ("Failed to strdup comment!\n"));
113 status = NT_STATUS_NO_MEMORY;
118 status = NT_STATUS_OK;
127 NTSTATUS printer_list_set_printer(TALLOC_CTX *mem_ctx,
132 struct db_context *db;
136 uint32_t time_h, time_l;
137 const char *str = NULL;
141 db = get_printer_list_db();
143 return NT_STATUS_INTERNAL_DB_CORRUPTION;
146 key = talloc_asprintf(mem_ctx, PL_KEY_FORMAT, name);
148 DEBUG(0, ("Failed to allocate key name!\n"));
149 return NT_STATUS_NO_MEMORY;
158 time_64 = last_refresh;
159 time_l = time_64 & 0xFFFFFFFFL;
160 time_h = time_64 >> 32;
162 len = tdb_pack(NULL, 0, PL_DATA_FORMAT, time_h, time_l, name, str);
164 data.dptr = talloc_array(key, uint8_t, len);
166 DEBUG(0, ("Failed to allocate tdb data buffer!\n"));
167 status = NT_STATUS_NO_MEMORY;
172 len = tdb_pack(data.dptr, data.dsize,
173 PL_DATA_FORMAT, time_h, time_l, name, str);
175 status = dbwrap_store_bystring_upper(db, key, data, TDB_REPLACE);
182 NTSTATUS printer_list_get_last_refresh(time_t *last_refresh)
184 struct db_context *db;
186 uint32_t time_h, time_l;
190 db = get_printer_list_db();
192 return NT_STATUS_INTERNAL_DB_CORRUPTION;
197 data = dbwrap_fetch_bystring(db, talloc_tos(), PL_TIMESTAMP_KEY);
198 if (data.dptr == NULL) {
199 DEBUG(1, ("Failed to fetch record!\n"));
200 status = NT_STATUS_NOT_FOUND;
204 ret = tdb_unpack(data.dptr, data.dsize,
205 PL_TSTAMP_FORMAT, &time_h, &time_l);
207 DEBUG(1, ("Failed to un pack printer data"));
208 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
212 *last_refresh = (time_t)(((uint64_t)time_h << 32) + time_l);
213 status = NT_STATUS_OK;
219 NTSTATUS printer_list_mark_reload(void)
221 struct db_context *db;
223 uint32_t time_h, time_l;
224 time_t now = time_mono(NULL);
228 db = get_printer_list_db();
230 return NT_STATUS_INTERNAL_DB_CORRUPTION;
233 time_l = ((uint64_t)now) & 0xFFFFFFFFL;
234 time_h = ((uint64_t)now) >> 32;
236 len = tdb_pack(NULL, 0, PL_TSTAMP_FORMAT, time_h, time_l);
238 data.dptr = talloc_array(talloc_tos(), uint8_t, len);
240 DEBUG(0, ("Failed to allocate tdb data buffer!\n"));
241 status = NT_STATUS_NO_MEMORY;
246 len = tdb_pack(data.dptr, data.dsize,
247 PL_TSTAMP_FORMAT, time_h, time_l);
249 status = dbwrap_store_bystring(db, PL_TIMESTAMP_KEY,
253 TALLOC_FREE(data.dptr);
257 typedef int (printer_list_trv_fn_t)(struct db_record *, void *);
259 static NTSTATUS printer_list_traverse(printer_list_trv_fn_t *fn,
262 struct db_context *db;
265 db = get_printer_list_db();
267 return NT_STATUS_INTERNAL_DB_CORRUPTION;
270 ret = db->traverse(db, fn, private_data);
272 return NT_STATUS_UNSUCCESSFUL;
278 struct printer_list_clean_state {
283 static int printer_list_clean_fn(struct db_record *rec, void *private_data)
285 struct printer_list_clean_state *state =
286 (struct printer_list_clean_state *)private_data;
287 uint32_t time_h, time_l;
293 /* skip anything that does not contain PL_DATA_FORMAT data */
294 if (strncmp((char *)rec->key.dptr,
295 PL_KEY_PREFIX, sizeof(PL_KEY_PREFIX)-1)) {
299 ret = tdb_unpack(rec->value.dptr, rec->value.dsize,
300 PL_DATA_FORMAT, &time_h, &time_l, &name, &comment);
302 DEBUG(1, ("Failed to un pack printer data"));
303 state->status = NT_STATUS_INTERNAL_DB_CORRUPTION;
310 refresh = (time_t)(((uint64_t)time_h << 32) + time_l);
312 if (refresh < state->last_refresh) {
313 state->status = rec->delete_rec(rec);
314 if (!NT_STATUS_IS_OK(state->status)) {
322 NTSTATUS printer_list_clean_old(void)
324 struct printer_list_clean_state state;
327 status = printer_list_get_last_refresh(&state.last_refresh);
328 if (!NT_STATUS_IS_OK(status)) {
332 state.status = NT_STATUS_OK;
334 status = printer_list_traverse(printer_list_clean_fn, &state);
335 if (NT_STATUS_EQUAL(status, NT_STATUS_UNSUCCESSFUL) &&
336 !NT_STATUS_IS_OK(state.status)) {
337 status = state.status;
343 struct printer_list_exec_state {
344 void (*fn)(const char *, const char *, void *);
349 static int printer_list_exec_fn(struct db_record *rec, void *private_data)
351 struct printer_list_exec_state *state =
352 (struct printer_list_exec_state *)private_data;
353 uint32_t time_h, time_l;
358 /* always skip PL_TIMESTAMP_KEY key */
359 if (strequal((const char *)rec->key.dptr, PL_TIMESTAMP_KEY)) {
363 ret = tdb_unpack(rec->value.dptr, rec->value.dsize,
364 PL_DATA_FORMAT, &time_h, &time_l, &name, &comment);
366 DEBUG(1, ("Failed to un pack printer data"));
367 state->status = NT_STATUS_INTERNAL_DB_CORRUPTION;
371 state->fn(name, comment, state->private_data);
378 NTSTATUS printer_list_run_fn(void (*fn)(const char *, const char *, void *),
381 struct printer_list_exec_state state;
385 state.private_data = private_data;
386 state.status = NT_STATUS_OK;
388 status = printer_list_traverse(printer_list_exec_fn, &state);
389 if (NT_STATUS_EQUAL(status, NT_STATUS_UNSUCCESSFUL) &&
390 !NT_STATUS_IS_OK(state.status)) {
391 status = state.status;