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"
24 #include "printer_list.h"
26 #define PL_DB_NAME() lock_path("printer_list.tdb")
27 #define PL_KEY_PREFIX "PRINTERLIST/PRN/"
28 #define PL_KEY_FORMAT PL_KEY_PREFIX"%s"
29 #define PL_TIMESTAMP_KEY "PRINTERLIST/GLOBAL/LAST_REFRESH"
30 #define PL_DATA_FORMAT "ddPP"
31 #define PL_TSTAMP_FORMAT "dd"
33 static struct db_context *get_printer_list_db(void)
35 static struct db_context *db;
40 db = db_open(NULL, PL_DB_NAME(), 0,
41 TDB_DEFAULT|TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH,
42 O_RDWR|O_CREAT, 0644);
46 bool printer_list_parent_init(void)
48 struct db_context *db;
51 * Open the tdb in the parent process (smbd) so that our
52 * CLEAR_IF_FIRST optimization in tdb_reopen_all can properly
56 db = get_printer_list_db();
58 DEBUG(1, ("could not open Printer List Database: %s\n",
65 NTSTATUS printer_list_get_printer(TALLOC_CTX *mem_ctx,
70 struct db_context *db;
73 uint32_t time_h, time_l;
79 db = get_printer_list_db();
81 return NT_STATUS_INTERNAL_DB_CORRUPTION;
84 key = talloc_asprintf(mem_ctx, PL_KEY_FORMAT, name);
86 DEBUG(0, ("Failed to allocate key name!\n"));
87 return NT_STATUS_NO_MEMORY;
90 data = dbwrap_fetch_bystring_upper(db, key, key);
91 if (data.dptr == NULL) {
92 DEBUG(1, ("Failed to fetch record!\n"));
93 status = NT_STATUS_NOT_FOUND;
97 ret = tdb_unpack(data.dptr, data.dsize,
99 &time_h, &time_l, &nstr, &cstr);
101 DEBUG(1, ("Failed to un pack printer data"));
102 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
107 *last_refresh = (time_t)(((uint64_t)time_h << 32) + time_l);
111 *comment = talloc_strdup(mem_ctx, cstr);
113 DEBUG(1, ("Failed to strdup comment!\n"));
114 status = NT_STATUS_NO_MEMORY;
119 status = NT_STATUS_OK;
128 NTSTATUS printer_list_set_printer(TALLOC_CTX *mem_ctx,
133 struct db_context *db;
137 uint32_t time_h, time_l;
138 const char *str = NULL;
142 db = get_printer_list_db();
144 return NT_STATUS_INTERNAL_DB_CORRUPTION;
147 key = talloc_asprintf(mem_ctx, PL_KEY_FORMAT, name);
149 DEBUG(0, ("Failed to allocate key name!\n"));
150 return NT_STATUS_NO_MEMORY;
159 time_64 = last_refresh;
160 time_l = time_64 & 0xFFFFFFFFL;
161 time_h = time_64 >> 32;
163 len = tdb_pack(NULL, 0, PL_DATA_FORMAT, time_h, time_l, name, str);
165 data.dptr = talloc_array(key, uint8_t, len);
167 DEBUG(0, ("Failed to allocate tdb data buffer!\n"));
168 status = NT_STATUS_NO_MEMORY;
173 len = tdb_pack(data.dptr, data.dsize,
174 PL_DATA_FORMAT, time_h, time_l, name, str);
176 status = dbwrap_store_bystring_upper(db, key, data, TDB_REPLACE);
183 NTSTATUS printer_list_get_last_refresh(time_t *last_refresh)
185 struct db_context *db;
187 uint32_t time_h, time_l;
191 db = get_printer_list_db();
193 return NT_STATUS_INTERNAL_DB_CORRUPTION;
198 data = dbwrap_fetch_bystring(db, talloc_tos(), PL_TIMESTAMP_KEY);
199 if (data.dptr == NULL) {
200 DEBUG(1, ("Failed to fetch record!\n"));
201 status = NT_STATUS_NOT_FOUND;
205 ret = tdb_unpack(data.dptr, data.dsize,
206 PL_TSTAMP_FORMAT, &time_h, &time_l);
208 DEBUG(1, ("Failed to un pack printer data"));
209 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
213 *last_refresh = (time_t)(((uint64_t)time_h << 32) + time_l);
214 status = NT_STATUS_OK;
220 NTSTATUS printer_list_mark_reload(void)
222 struct db_context *db;
224 uint32_t time_h, time_l;
225 time_t now = time_mono(NULL);
229 db = get_printer_list_db();
231 return NT_STATUS_INTERNAL_DB_CORRUPTION;
234 time_l = ((uint64_t)now) & 0xFFFFFFFFL;
235 time_h = ((uint64_t)now) >> 32;
237 len = tdb_pack(NULL, 0, PL_TSTAMP_FORMAT, time_h, time_l);
239 data.dptr = talloc_array(talloc_tos(), uint8_t, len);
241 DEBUG(0, ("Failed to allocate tdb data buffer!\n"));
242 status = NT_STATUS_NO_MEMORY;
247 len = tdb_pack(data.dptr, data.dsize,
248 PL_TSTAMP_FORMAT, time_h, time_l);
250 status = dbwrap_store_bystring(db, PL_TIMESTAMP_KEY,
254 TALLOC_FREE(data.dptr);
258 typedef int (printer_list_trv_fn_t)(struct db_record *, void *);
260 static NTSTATUS printer_list_traverse(printer_list_trv_fn_t *fn,
263 struct db_context *db;
266 db = get_printer_list_db();
268 return NT_STATUS_INTERNAL_DB_CORRUPTION;
271 ret = db->traverse(db, fn, private_data);
273 return NT_STATUS_UNSUCCESSFUL;
279 struct printer_list_clean_state {
284 static int printer_list_clean_fn(struct db_record *rec, void *private_data)
286 struct printer_list_clean_state *state =
287 (struct printer_list_clean_state *)private_data;
288 uint32_t time_h, time_l;
294 /* skip anything that does not contain PL_DATA_FORMAT data */
295 if (strncmp((char *)rec->key.dptr,
296 PL_KEY_PREFIX, sizeof(PL_KEY_PREFIX)-1)) {
300 ret = tdb_unpack(rec->value.dptr, rec->value.dsize,
301 PL_DATA_FORMAT, &time_h, &time_l, &name, &comment);
303 DEBUG(1, ("Failed to un pack printer data"));
304 state->status = NT_STATUS_INTERNAL_DB_CORRUPTION;
311 refresh = (time_t)(((uint64_t)time_h << 32) + time_l);
313 if (refresh < state->last_refresh) {
314 state->status = rec->delete_rec(rec);
315 if (!NT_STATUS_IS_OK(state->status)) {
323 NTSTATUS printer_list_clean_old(void)
325 struct printer_list_clean_state state;
328 status = printer_list_get_last_refresh(&state.last_refresh);
329 if (!NT_STATUS_IS_OK(status)) {
333 state.status = NT_STATUS_OK;
335 status = printer_list_traverse(printer_list_clean_fn, &state);
336 if (NT_STATUS_EQUAL(status, NT_STATUS_UNSUCCESSFUL) &&
337 !NT_STATUS_IS_OK(state.status)) {
338 status = state.status;
344 struct printer_list_exec_state {
345 void (*fn)(const char *, const char *, void *);
350 static int printer_list_exec_fn(struct db_record *rec, void *private_data)
352 struct printer_list_exec_state *state =
353 (struct printer_list_exec_state *)private_data;
354 uint32_t time_h, time_l;
359 /* always skip PL_TIMESTAMP_KEY key */
360 if (strequal((const char *)rec->key.dptr, PL_TIMESTAMP_KEY)) {
364 ret = tdb_unpack(rec->value.dptr, rec->value.dsize,
365 PL_DATA_FORMAT, &time_h, &time_l, &name, &comment);
367 DEBUG(1, ("Failed to un pack printer data"));
368 state->status = NT_STATUS_INTERNAL_DB_CORRUPTION;
372 state->fn(name, comment, state->private_data);
379 NTSTATUS printer_list_run_fn(void (*fn)(const char *, const char *, void *),
382 struct printer_list_exec_state state;
386 state.private_data = private_data;
387 state.status = NT_STATUS_OK;
389 status = printer_list_traverse(printer_list_exec_fn, &state);
390 if (NT_STATUS_EQUAL(status, NT_STATUS_UNSUCCESSFUL) &&
391 !NT_STATUS_IS_OK(state.status)) {
392 status = state.status;