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
*printerlist_db
;
35 static struct db_context
*get_printer_list_db(void)
39 if (printerlist_db
!= NULL
) {
40 return printerlist_db
;
43 db_path
= lock_path(talloc_tos(), "printer_list.tdb");
44 if (db_path
== NULL
) {
48 printerlist_db
= db_open(NULL
,
51 TDB_DEFAULT
|TDB_INCOMPATIBLE_HASH
,
57 if (printerlist_db
== NULL
) {
58 DBG_ERR("Failed to open printer_list.tdb\n");
60 return printerlist_db
;
63 NTSTATUS
printer_list_get_printer(TALLOC_CTX
*mem_ctx
,
66 const char **location
,
69 struct db_context
*db
;
72 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 status
= dbwrap_fetch_bystring_upper(db
, key
, key
, &data
);
91 if (!NT_STATUS_IS_OK(status
)) {
92 DEBUG(6, ("Failed to fetch record! "
93 "The printer database is empty?\n"));
97 ret
= tdb_unpack(data
.dptr
, data
.dsize
,
99 &time_h
, &time_l
, &nstr
, &cstr
, &lstr
);
101 DEBUG(1, ("Failed to unpack printer data\n"));
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
;
120 *location
= talloc_strdup(mem_ctx
, lstr
);
121 if (*location
== NULL
) {
122 DEBUG(1, ("Failed to strdup location!\n"));
123 status
= NT_STATUS_NO_MEMORY
;
128 status
= NT_STATUS_OK
;
138 bool printer_list_printername_exists(const char *name
)
140 struct db_context
*db
= get_printer_list_db();
148 key
= talloc_asprintf_strupper_m(
149 talloc_tos(), PL_KEY_FORMAT
, name
);
154 ok
= dbwrap_exists(db
, string_term_tdb_data(key
));
159 NTSTATUS
printer_list_set_printer(TALLOC_CTX
*mem_ctx
,
162 const char *location
,
165 struct db_context
*db
;
169 uint32_t time_h
, time_l
;
173 db
= get_printer_list_db();
175 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
178 key
= talloc_asprintf(mem_ctx
, PL_KEY_FORMAT
, name
);
180 DEBUG(0, ("Failed to allocate key name!\n"));
181 return NT_STATUS_NO_MEMORY
;
184 if (comment
== NULL
) {
188 if (location
== NULL
) {
192 time_64
= last_refresh
;
193 time_l
= time_64
& 0xFFFFFFFFL
;
194 time_h
= time_64
>> 32;
196 len
= tdb_pack(NULL
, 0,
204 data
.dptr
= talloc_array(key
, uint8_t, len
);
206 DEBUG(0, ("Failed to allocate tdb data buffer!\n"));
207 status
= NT_STATUS_NO_MEMORY
;
212 len
= tdb_pack(data
.dptr
, data
.dsize
,
220 status
= dbwrap_store_bystring_upper(db
, key
, data
, TDB_REPLACE
);
227 NTSTATUS
printer_list_get_last_refresh(time_t *last_refresh
)
229 struct db_context
*db
;
231 uint32_t time_h
, time_l
;
235 db
= get_printer_list_db();
237 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
242 status
= dbwrap_fetch_bystring(db
, talloc_tos(), PL_TIMESTAMP_KEY
, &data
);
243 if (!NT_STATUS_IS_OK(status
)) {
244 DEBUG(1, ("Failed to fetch record!\n"));
248 ret
= tdb_unpack(data
.dptr
, data
.dsize
,
249 PL_TSTAMP_FORMAT
, &time_h
, &time_l
);
250 TALLOC_FREE(data
.dptr
);
252 DEBUG(1, ("Failed to unpack printer data\n"));
253 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
257 *last_refresh
= (time_t)(((uint64_t)time_h
<< 32) + time_l
);
258 status
= NT_STATUS_OK
;
264 NTSTATUS
printer_list_mark_reload(void)
266 struct db_context
*db
;
268 uint32_t time_h
, time_l
;
269 time_t now
= time_mono(NULL
);
273 db
= get_printer_list_db();
275 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
278 time_l
= ((uint64_t)now
) & 0xFFFFFFFFL
;
279 time_h
= ((uint64_t)now
) >> 32;
281 len
= tdb_pack(NULL
, 0, PL_TSTAMP_FORMAT
, time_h
, time_l
);
283 data
.dptr
= talloc_array(talloc_tos(), uint8_t, len
);
285 DEBUG(0, ("Failed to allocate tdb data buffer!\n"));
286 status
= NT_STATUS_NO_MEMORY
;
291 len
= tdb_pack(data
.dptr
, data
.dsize
,
292 PL_TSTAMP_FORMAT
, time_h
, time_l
);
294 status
= dbwrap_store_bystring(db
, PL_TIMESTAMP_KEY
,
298 TALLOC_FREE(data
.dptr
);
302 typedef int (printer_list_trv_fn_t
)(struct db_record
*, void *);
304 static NTSTATUS
printer_list_traverse(printer_list_trv_fn_t
*fn
,
308 struct db_context
*db
;
311 db
= get_printer_list_db();
313 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
317 status
= dbwrap_traverse_read(db
, fn
, private_data
, NULL
);
319 status
= dbwrap_traverse(db
, fn
, private_data
, NULL
);
325 struct printer_list_clean_state
{
330 static int printer_list_clean_fn(struct db_record
*rec
, void *private_data
)
332 struct printer_list_clean_state
*state
=
333 (struct printer_list_clean_state
*)private_data
;
334 uint32_t time_h
, time_l
;
343 key
= dbwrap_record_get_key(rec
);
345 /* skip anything that does not contain PL_DATA_FORMAT data */
346 if (strncmp((char *)key
.dptr
,
347 PL_KEY_PREFIX
, sizeof(PL_KEY_PREFIX
)-1)) {
351 value
= dbwrap_record_get_value(rec
);
353 ret
= tdb_unpack(value
.dptr
, value
.dsize
,
354 PL_DATA_FORMAT
, &time_h
, &time_l
, &name
, &comment
,
357 DEBUG(1, ("Failed to unpack printer data\n"));
358 state
->status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
366 refresh
= (time_t)(((uint64_t)time_h
<< 32) + time_l
);
368 if (refresh
< state
->last_refresh
) {
369 state
->status
= dbwrap_record_delete(rec
);
370 if (!NT_STATUS_IS_OK(state
->status
)) {
378 NTSTATUS
printer_list_clean_old(void)
380 struct printer_list_clean_state state
;
383 status
= printer_list_get_last_refresh(&state
.last_refresh
);
384 if (!NT_STATUS_IS_OK(status
)) {
388 state
.status
= NT_STATUS_OK
;
390 status
= printer_list_traverse(printer_list_clean_fn
, &state
, false);
391 if (NT_STATUS_EQUAL(status
, NT_STATUS_UNSUCCESSFUL
) &&
392 !NT_STATUS_IS_OK(state
.status
)) {
393 status
= state
.status
;
399 struct printer_list_exec_state
{
400 void (*fn
)(const char *, const char *, const char *, void *);
405 static int printer_list_exec_fn(struct db_record
*rec
, void *private_data
)
407 struct printer_list_exec_state
*state
=
408 (struct printer_list_exec_state
*)private_data
;
409 uint32_t time_h
, time_l
;
417 key
= dbwrap_record_get_key(rec
);
419 /* always skip PL_TIMESTAMP_KEY key */
420 if (strequal((const char *)key
.dptr
, PL_TIMESTAMP_KEY
)) {
424 value
= dbwrap_record_get_value(rec
);
426 ret
= tdb_unpack(value
.dptr
, value
.dsize
,
427 PL_DATA_FORMAT
, &time_h
, &time_l
, &name
, &comment
,
430 DEBUG(1, ("Failed to unpack printer data\n"));
431 state
->status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
435 state
->fn(name
, comment
, location
, state
->private_data
);
443 NTSTATUS
printer_list_read_run_fn(void (*fn
)(const char *, const char *, const char *, void *),
446 struct printer_list_exec_state state
;
450 state
.private_data
= private_data
;
451 state
.status
= NT_STATUS_OK
;
453 status
= printer_list_traverse(printer_list_exec_fn
, &state
, true);
454 if (NT_STATUS_EQUAL(status
, NT_STATUS_UNSUCCESSFUL
) &&
455 !NT_STATUS_IS_OK(state
.status
)) {
456 status
= state
.status
;