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"
22 #include "dbwrap/dbwrap.h"
23 #include "dbwrap/dbwrap_open.h"
25 #include "printer_list.h"
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 "ddPPP"
31 #define PL_TSTAMP_FORMAT "dd"
33 static struct db_context
*get_printer_list_db(void)
35 static struct db_context
*db
;
42 db_path
= lock_path(talloc_tos(), "printer_list.tdb");
43 if (db_path
== NULL
) {
47 db
= db_open(NULL
, db_path
, 0,
48 TDB_DEFAULT
|TDB_CLEAR_IF_FIRST
|TDB_INCOMPATIBLE_HASH
,
49 O_RDWR
|O_CREAT
, 0644, DBWRAP_LOCK_ORDER_1
,
55 bool printer_list_parent_init(void)
57 struct db_context
*db
;
60 * Open the tdb in the parent process (smbd) so that our
61 * CLEAR_IF_FIRST optimization in tdb_reopen_all can properly
65 db
= get_printer_list_db();
67 DEBUG(1, ("could not open Printer List Database: %s\n",
74 NTSTATUS
printer_list_get_printer(TALLOC_CTX
*mem_ctx
,
77 const char **location
,
80 struct db_context
*db
;
83 uint32_t time_h
, time_l
;
90 db
= get_printer_list_db();
92 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
95 key
= talloc_asprintf(mem_ctx
, PL_KEY_FORMAT
, name
);
97 DEBUG(0, ("Failed to allocate key name!\n"));
98 return NT_STATUS_NO_MEMORY
;
101 status
= dbwrap_fetch_bystring_upper(db
, key
, key
, &data
);
102 if (!NT_STATUS_IS_OK(status
)) {
103 DEBUG(6, ("Failed to fetch record! "
104 "The printer database is empty?\n"));
108 ret
= tdb_unpack(data
.dptr
, data
.dsize
,
110 &time_h
, &time_l
, &nstr
, &cstr
, &lstr
);
112 DEBUG(1, ("Failed to un pack printer data"));
113 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
118 *last_refresh
= (time_t)(((uint64_t)time_h
<< 32) + time_l
);
122 *comment
= talloc_strdup(mem_ctx
, cstr
);
124 DEBUG(1, ("Failed to strdup comment!\n"));
125 status
= NT_STATUS_NO_MEMORY
;
131 *location
= talloc_strdup(mem_ctx
, lstr
);
132 if (*location
== NULL
) {
133 DEBUG(1, ("Failed to strdup location!\n"));
134 status
= NT_STATUS_NO_MEMORY
;
139 status
= NT_STATUS_OK
;
149 bool printer_list_printername_exists(const char *name
)
151 struct db_context
*db
= get_printer_list_db();
159 key
= talloc_asprintf_strupper_m(
160 talloc_tos(), PL_KEY_FORMAT
, name
);
165 ok
= dbwrap_exists(db
, string_term_tdb_data(key
));
170 NTSTATUS
printer_list_set_printer(TALLOC_CTX
*mem_ctx
,
173 const char *location
,
176 struct db_context
*db
;
180 uint32_t time_h
, time_l
;
184 db
= get_printer_list_db();
186 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
189 key
= talloc_asprintf(mem_ctx
, PL_KEY_FORMAT
, name
);
191 DEBUG(0, ("Failed to allocate key name!\n"));
192 return NT_STATUS_NO_MEMORY
;
195 if (comment
== NULL
) {
199 if (location
== NULL
) {
203 time_64
= last_refresh
;
204 time_l
= time_64
& 0xFFFFFFFFL
;
205 time_h
= time_64
>> 32;
207 len
= tdb_pack(NULL
, 0,
215 data
.dptr
= talloc_array(key
, uint8_t, len
);
217 DEBUG(0, ("Failed to allocate tdb data buffer!\n"));
218 status
= NT_STATUS_NO_MEMORY
;
223 len
= tdb_pack(data
.dptr
, data
.dsize
,
231 status
= dbwrap_store_bystring_upper(db
, key
, data
, TDB_REPLACE
);
238 NTSTATUS
printer_list_get_last_refresh(time_t *last_refresh
)
240 struct db_context
*db
;
242 uint32_t time_h
, time_l
;
246 db
= get_printer_list_db();
248 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
253 status
= dbwrap_fetch_bystring(db
, talloc_tos(), PL_TIMESTAMP_KEY
, &data
);
254 if (!NT_STATUS_IS_OK(status
)) {
255 DEBUG(1, ("Failed to fetch record!\n"));
259 ret
= tdb_unpack(data
.dptr
, data
.dsize
,
260 PL_TSTAMP_FORMAT
, &time_h
, &time_l
);
261 TALLOC_FREE(data
.dptr
);
263 DEBUG(1, ("Failed to un pack printer data"));
264 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
268 *last_refresh
= (time_t)(((uint64_t)time_h
<< 32) + time_l
);
269 status
= NT_STATUS_OK
;
275 NTSTATUS
printer_list_mark_reload(void)
277 struct db_context
*db
;
279 uint32_t time_h
, time_l
;
280 time_t now
= time_mono(NULL
);
284 db
= get_printer_list_db();
286 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
289 time_l
= ((uint64_t)now
) & 0xFFFFFFFFL
;
290 time_h
= ((uint64_t)now
) >> 32;
292 len
= tdb_pack(NULL
, 0, PL_TSTAMP_FORMAT
, time_h
, time_l
);
294 data
.dptr
= talloc_array(talloc_tos(), uint8_t, len
);
296 DEBUG(0, ("Failed to allocate tdb data buffer!\n"));
297 status
= NT_STATUS_NO_MEMORY
;
302 len
= tdb_pack(data
.dptr
, data
.dsize
,
303 PL_TSTAMP_FORMAT
, time_h
, time_l
);
305 status
= dbwrap_store_bystring(db
, PL_TIMESTAMP_KEY
,
309 TALLOC_FREE(data
.dptr
);
313 typedef int (printer_list_trv_fn_t
)(struct db_record
*, void *);
315 static NTSTATUS
printer_list_traverse(printer_list_trv_fn_t
*fn
,
319 struct db_context
*db
;
322 db
= get_printer_list_db();
324 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
328 status
= dbwrap_traverse_read(db
, fn
, private_data
, NULL
);
330 status
= dbwrap_traverse(db
, fn
, private_data
, NULL
);
336 struct printer_list_clean_state
{
341 static int printer_list_clean_fn(struct db_record
*rec
, void *private_data
)
343 struct printer_list_clean_state
*state
=
344 (struct printer_list_clean_state
*)private_data
;
345 uint32_t time_h
, time_l
;
354 key
= dbwrap_record_get_key(rec
);
356 /* skip anything that does not contain PL_DATA_FORMAT data */
357 if (strncmp((char *)key
.dptr
,
358 PL_KEY_PREFIX
, sizeof(PL_KEY_PREFIX
)-1)) {
362 value
= dbwrap_record_get_value(rec
);
364 ret
= tdb_unpack(value
.dptr
, value
.dsize
,
365 PL_DATA_FORMAT
, &time_h
, &time_l
, &name
, &comment
,
368 DEBUG(1, ("Failed to un pack printer data"));
369 state
->status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
377 refresh
= (time_t)(((uint64_t)time_h
<< 32) + time_l
);
379 if (refresh
< state
->last_refresh
) {
380 state
->status
= dbwrap_record_delete(rec
);
381 if (!NT_STATUS_IS_OK(state
->status
)) {
389 NTSTATUS
printer_list_clean_old(void)
391 struct printer_list_clean_state state
;
394 status
= printer_list_get_last_refresh(&state
.last_refresh
);
395 if (!NT_STATUS_IS_OK(status
)) {
399 state
.status
= NT_STATUS_OK
;
401 status
= printer_list_traverse(printer_list_clean_fn
, &state
, false);
402 if (NT_STATUS_EQUAL(status
, NT_STATUS_UNSUCCESSFUL
) &&
403 !NT_STATUS_IS_OK(state
.status
)) {
404 status
= state
.status
;
410 struct printer_list_exec_state
{
411 void (*fn
)(const char *, const char *, const char *, void *);
416 static int printer_list_exec_fn(struct db_record
*rec
, void *private_data
)
418 struct printer_list_exec_state
*state
=
419 (struct printer_list_exec_state
*)private_data
;
420 uint32_t time_h
, time_l
;
428 key
= dbwrap_record_get_key(rec
);
430 /* always skip PL_TIMESTAMP_KEY key */
431 if (strequal((const char *)key
.dptr
, PL_TIMESTAMP_KEY
)) {
435 value
= dbwrap_record_get_value(rec
);
437 ret
= tdb_unpack(value
.dptr
, value
.dsize
,
438 PL_DATA_FORMAT
, &time_h
, &time_l
, &name
, &comment
,
441 DEBUG(1, ("Failed to un pack printer data"));
442 state
->status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
446 state
->fn(name
, comment
, location
, state
->private_data
);
454 NTSTATUS
printer_list_read_run_fn(void (*fn
)(const char *, const char *, const char *, void *),
457 struct printer_list_exec_state state
;
461 state
.private_data
= private_data
;
462 state
.status
= NT_STATUS_OK
;
464 status
= printer_list_traverse(printer_list_exec_fn
, &state
, true);
465 if (NT_STATUS_EQUAL(status
, NT_STATUS_UNSUCCESSFUL
) &&
466 !NT_STATUS_IS_OK(state
.status
)) {
467 status
= state
.status
;