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("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 NTSTATUS
printer_list_set_printer(TALLOC_CTX
*mem_ctx
,
152 const char *location
,
155 struct db_context
*db
;
159 uint32_t time_h
, time_l
;
163 db
= get_printer_list_db();
165 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
168 key
= talloc_asprintf(mem_ctx
, PL_KEY_FORMAT
, name
);
170 DEBUG(0, ("Failed to allocate key name!\n"));
171 return NT_STATUS_NO_MEMORY
;
174 if (comment
== NULL
) {
178 if (location
== NULL
) {
182 time_64
= last_refresh
;
183 time_l
= time_64
& 0xFFFFFFFFL
;
184 time_h
= time_64
>> 32;
186 len
= tdb_pack(NULL
, 0,
194 data
.dptr
= talloc_array(key
, uint8_t, len
);
196 DEBUG(0, ("Failed to allocate tdb data buffer!\n"));
197 status
= NT_STATUS_NO_MEMORY
;
202 len
= tdb_pack(data
.dptr
, data
.dsize
,
210 status
= dbwrap_store_bystring_upper(db
, key
, data
, TDB_REPLACE
);
217 NTSTATUS
printer_list_get_last_refresh(time_t *last_refresh
)
219 struct db_context
*db
;
221 uint32_t time_h
, time_l
;
225 db
= get_printer_list_db();
227 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
232 status
= dbwrap_fetch_bystring(db
, talloc_tos(), PL_TIMESTAMP_KEY
, &data
);
233 if (!NT_STATUS_IS_OK(status
)) {
234 DEBUG(1, ("Failed to fetch record!\n"));
238 ret
= tdb_unpack(data
.dptr
, data
.dsize
,
239 PL_TSTAMP_FORMAT
, &time_h
, &time_l
);
240 TALLOC_FREE(data
.dptr
);
242 DEBUG(1, ("Failed to un pack printer data"));
243 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
247 *last_refresh
= (time_t)(((uint64_t)time_h
<< 32) + time_l
);
248 status
= NT_STATUS_OK
;
254 NTSTATUS
printer_list_mark_reload(void)
256 struct db_context
*db
;
258 uint32_t time_h
, time_l
;
259 time_t now
= time_mono(NULL
);
263 db
= get_printer_list_db();
265 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
268 time_l
= ((uint64_t)now
) & 0xFFFFFFFFL
;
269 time_h
= ((uint64_t)now
) >> 32;
271 len
= tdb_pack(NULL
, 0, PL_TSTAMP_FORMAT
, time_h
, time_l
);
273 data
.dptr
= talloc_array(talloc_tos(), uint8_t, len
);
275 DEBUG(0, ("Failed to allocate tdb data buffer!\n"));
276 status
= NT_STATUS_NO_MEMORY
;
281 len
= tdb_pack(data
.dptr
, data
.dsize
,
282 PL_TSTAMP_FORMAT
, time_h
, time_l
);
284 status
= dbwrap_store_bystring(db
, PL_TIMESTAMP_KEY
,
288 TALLOC_FREE(data
.dptr
);
292 typedef int (printer_list_trv_fn_t
)(struct db_record
*, void *);
294 static NTSTATUS
printer_list_traverse(printer_list_trv_fn_t
*fn
,
298 struct db_context
*db
;
301 db
= get_printer_list_db();
303 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
307 status
= dbwrap_traverse_read(db
, fn
, private_data
, NULL
);
309 status
= dbwrap_traverse(db
, fn
, private_data
, NULL
);
315 struct printer_list_clean_state
{
320 static int printer_list_clean_fn(struct db_record
*rec
, void *private_data
)
322 struct printer_list_clean_state
*state
=
323 (struct printer_list_clean_state
*)private_data
;
324 uint32_t time_h
, time_l
;
333 key
= dbwrap_record_get_key(rec
);
335 /* skip anything that does not contain PL_DATA_FORMAT data */
336 if (strncmp((char *)key
.dptr
,
337 PL_KEY_PREFIX
, sizeof(PL_KEY_PREFIX
)-1)) {
341 value
= dbwrap_record_get_value(rec
);
343 ret
= tdb_unpack(value
.dptr
, value
.dsize
,
344 PL_DATA_FORMAT
, &time_h
, &time_l
, &name
, &comment
,
347 DEBUG(1, ("Failed to un pack printer data"));
348 state
->status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
356 refresh
= (time_t)(((uint64_t)time_h
<< 32) + time_l
);
358 if (refresh
< state
->last_refresh
) {
359 state
->status
= dbwrap_record_delete(rec
);
360 if (!NT_STATUS_IS_OK(state
->status
)) {
368 NTSTATUS
printer_list_clean_old(void)
370 struct printer_list_clean_state state
;
373 status
= printer_list_get_last_refresh(&state
.last_refresh
);
374 if (!NT_STATUS_IS_OK(status
)) {
378 state
.status
= NT_STATUS_OK
;
380 status
= printer_list_traverse(printer_list_clean_fn
, &state
, false);
381 if (NT_STATUS_EQUAL(status
, NT_STATUS_UNSUCCESSFUL
) &&
382 !NT_STATUS_IS_OK(state
.status
)) {
383 status
= state
.status
;
389 struct printer_list_exec_state
{
390 void (*fn
)(const char *, const char *, const char *, void *);
395 static int printer_list_exec_fn(struct db_record
*rec
, void *private_data
)
397 struct printer_list_exec_state
*state
=
398 (struct printer_list_exec_state
*)private_data
;
399 uint32_t time_h
, time_l
;
407 key
= dbwrap_record_get_key(rec
);
409 /* always skip PL_TIMESTAMP_KEY key */
410 if (strequal((const char *)key
.dptr
, PL_TIMESTAMP_KEY
)) {
414 value
= dbwrap_record_get_value(rec
);
416 ret
= tdb_unpack(value
.dptr
, value
.dsize
,
417 PL_DATA_FORMAT
, &time_h
, &time_l
, &name
, &comment
,
420 DEBUG(1, ("Failed to un pack printer data"));
421 state
->status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
425 state
->fn(name
, comment
, location
, state
->private_data
);
433 NTSTATUS
printer_list_read_run_fn(void (*fn
)(const char *, const char *, const char *, void *),
436 struct printer_list_exec_state state
;
440 state
.private_data
= private_data
;
441 state
.status
= NT_STATUS_OK
;
443 status
= printer_list_traverse(printer_list_exec_fn
, &state
, true);
444 if (NT_STATUS_EQUAL(status
, NT_STATUS_UNSUCCESSFUL
) &&
445 !NT_STATUS_IS_OK(state
.status
)) {
446 status
= state
.status
;