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/>.
22 #include "printer_list.h"
24 #define PL_DB_NAME() lock_path("printer_list.tdb")
25 #define PL_KEY_PREFIX "PRINTERLIST/PRN/"
26 #define PL_KEY_FORMAT PL_KEY_PREFIX"%s"
27 #define PL_TIMESTAMP_KEY "PRINTERLIST/GLOBAL/LAST_REFRESH"
28 #define PL_DATA_FORMAT "ddPP"
29 #define PL_TSTAMP_FORMAT "dd"
31 static struct db_context
*get_printer_list_db(void)
33 static struct db_context
*db
;
38 db
= db_open(NULL
, PL_DB_NAME(), 0,
39 TDB_DEFAULT
|TDB_CLEAR_IF_FIRST
|TDB_INCOMPATIBLE_HASH
,
40 O_RDWR
|O_CREAT
, 0644);
44 bool printer_list_parent_init(void)
46 struct db_context
*db
;
49 * Open the tdb in the parent process (smbd) so that our
50 * CLEAR_IF_FIRST optimization in tdb_reopen_all can properly
54 db
= get_printer_list_db();
56 DEBUG(1, ("could not open Printer List Database: %s\n",
63 NTSTATUS
printer_list_get_printer(TALLOC_CTX
*mem_ctx
,
68 struct db_context
*db
;
71 uint32_t time_h
, time_l
;
77 db
= get_printer_list_db();
79 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
82 key
= talloc_asprintf(mem_ctx
, PL_KEY_FORMAT
, name
);
84 DEBUG(0, ("Failed to allocate key name!\n"));
85 return NT_STATUS_NO_MEMORY
;
88 data
= dbwrap_fetch_bystring_upper(db
, key
, key
);
89 if (data
.dptr
== NULL
) {
90 DEBUG(1, ("Failed to fetch record!\n"));
91 status
= NT_STATUS_NOT_FOUND
;
95 ret
= tdb_unpack(data
.dptr
, data
.dsize
,
97 &time_h
, &time_l
, &nstr
, &cstr
);
99 DEBUG(1, ("Failed to un pack printer data"));
100 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
105 *last_refresh
= (time_t)(((uint64_t)time_h
<< 32) + time_l
);
109 *comment
= talloc_strdup(mem_ctx
, cstr
);
111 DEBUG(1, ("Failed to strdup comment!\n"));
112 status
= NT_STATUS_NO_MEMORY
;
117 status
= NT_STATUS_OK
;
126 NTSTATUS
printer_list_set_printer(TALLOC_CTX
*mem_ctx
,
131 struct db_context
*db
;
135 uint32_t time_h
, time_l
;
136 const char *str
= NULL
;
140 db
= get_printer_list_db();
142 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
145 key
= talloc_asprintf(mem_ctx
, PL_KEY_FORMAT
, name
);
147 DEBUG(0, ("Failed to allocate key name!\n"));
148 return NT_STATUS_NO_MEMORY
;
157 time_64
= last_refresh
;
158 time_l
= time_64
& 0xFFFFFFFFL
;
159 time_h
= time_64
>> 32;
161 len
= tdb_pack(NULL
, 0, PL_DATA_FORMAT
, time_h
, time_l
, name
, str
);
163 data
.dptr
= talloc_array(key
, uint8_t, len
);
165 DEBUG(0, ("Failed to allocate tdb data buffer!\n"));
166 status
= NT_STATUS_NO_MEMORY
;
171 len
= tdb_pack(data
.dptr
, data
.dsize
,
172 PL_DATA_FORMAT
, time_h
, time_l
, name
, str
);
174 status
= dbwrap_store_bystring_upper(db
, key
, data
, TDB_REPLACE
);
181 NTSTATUS
printer_list_get_last_refresh(time_t *last_refresh
)
183 struct db_context
*db
;
185 uint32_t time_h
, time_l
;
189 db
= get_printer_list_db();
191 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
196 data
= dbwrap_fetch_bystring(db
, talloc_tos(), PL_TIMESTAMP_KEY
);
197 if (data
.dptr
== NULL
) {
198 DEBUG(1, ("Failed to fetch record!\n"));
199 status
= NT_STATUS_NOT_FOUND
;
203 ret
= tdb_unpack(data
.dptr
, data
.dsize
,
204 PL_TSTAMP_FORMAT
, &time_h
, &time_l
);
206 DEBUG(1, ("Failed to un pack printer data"));
207 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
211 *last_refresh
= (time_t)(((uint64_t)time_h
<< 32) + time_l
);
212 status
= NT_STATUS_OK
;
218 bool printer_list_need_refresh(void)
224 status
= printer_list_get_last_refresh(&last_refresh
);
225 if (!NT_STATUS_IS_OK(status
)) {
228 timediff
= time_mono(NULL
) - last_refresh
;
231 /* if refresh occurred more than 1s (TODO:use lp_printcap_cache_time) ago,
232 * then we need to refresh */
234 } else if (timediff
< 0) {
235 /* last_refresh newer than now. Seems we have no monotonic
236 * clock and the clock was adjusted backwards.
237 * we need to refresh which also resets last_refresh */
244 NTSTATUS
printer_list_mark_reload(void)
246 struct db_context
*db
;
248 uint32_t time_h
, time_l
;
249 time_t now
= time_mono(NULL
);
253 db
= get_printer_list_db();
255 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
258 time_l
= ((uint64_t)now
) & 0xFFFFFFFFL
;
259 time_h
= ((uint64_t)now
) >> 32;
261 len
= tdb_pack(NULL
, 0, PL_TSTAMP_FORMAT
, time_h
, time_l
);
263 data
.dptr
= talloc_array(talloc_tos(), uint8_t, len
);
265 DEBUG(0, ("Failed to allocate tdb data buffer!\n"));
266 status
= NT_STATUS_NO_MEMORY
;
271 len
= tdb_pack(data
.dptr
, data
.dsize
,
272 PL_TSTAMP_FORMAT
, time_h
, time_l
);
274 status
= dbwrap_store_bystring(db
, PL_TIMESTAMP_KEY
,
278 TALLOC_FREE(data
.dptr
);
282 typedef int (printer_list_trv_fn_t
)(struct db_record
*, void *);
284 static NTSTATUS
printer_list_traverse(printer_list_trv_fn_t
*fn
,
287 struct db_context
*db
;
290 db
= get_printer_list_db();
292 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
295 ret
= db
->traverse(db
, fn
, private_data
);
297 return NT_STATUS_UNSUCCESSFUL
;
303 struct printer_list_clean_state
{
308 static int printer_list_clean_fn(struct db_record
*rec
, void *private_data
)
310 struct printer_list_clean_state
*state
=
311 (struct printer_list_clean_state
*)private_data
;
312 uint32_t time_h
, time_l
;
318 /* skip anything that does not contain PL_DATA_FORMAT data */
319 if (strncmp((char *)rec
->key
.dptr
,
320 PL_KEY_PREFIX
, sizeof(PL_KEY_PREFIX
)-1)) {
324 ret
= tdb_unpack(rec
->value
.dptr
, rec
->value
.dsize
,
325 PL_DATA_FORMAT
, &time_h
, &time_l
, &name
, &comment
);
327 DEBUG(1, ("Failed to un pack printer data"));
328 state
->status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
335 refresh
= (time_t)(((uint64_t)time_h
<< 32) + time_l
);
337 if (refresh
< state
->last_refresh
) {
338 state
->status
= rec
->delete_rec(rec
);
339 if (!NT_STATUS_IS_OK(state
->status
)) {
347 NTSTATUS
printer_list_clean_old(void)
349 struct printer_list_clean_state state
;
352 status
= printer_list_get_last_refresh(&state
.last_refresh
);
353 if (!NT_STATUS_IS_OK(status
)) {
357 state
.status
= NT_STATUS_OK
;
359 status
= printer_list_traverse(printer_list_clean_fn
, &state
);
360 if (NT_STATUS_EQUAL(status
, NT_STATUS_UNSUCCESSFUL
) &&
361 !NT_STATUS_IS_OK(state
.status
)) {
362 status
= state
.status
;
368 struct printer_list_exec_state
{
369 void (*fn
)(const char *, const char *, void *);
374 static int printer_list_exec_fn(struct db_record
*rec
, void *private_data
)
376 struct printer_list_exec_state
*state
=
377 (struct printer_list_exec_state
*)private_data
;
378 uint32_t time_h
, time_l
;
383 /* always skip PL_TIMESTAMP_KEY key */
384 if (strequal((const char *)rec
->key
.dptr
, PL_TIMESTAMP_KEY
)) {
388 ret
= tdb_unpack(rec
->value
.dptr
, rec
->value
.dsize
,
389 PL_DATA_FORMAT
, &time_h
, &time_l
, &name
, &comment
);
391 DEBUG(1, ("Failed to un pack printer data"));
392 state
->status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
396 state
->fn(name
, comment
, state
->private_data
);
403 NTSTATUS
printer_list_run_fn(void (*fn
)(const char *, const char *, void *),
406 struct printer_list_exec_state state
;
410 state
.private_data
= private_data
;
411 state
.status
= NT_STATUS_OK
;
413 status
= printer_list_traverse(printer_list_exec_fn
, &state
);
414 if (NT_STATUS_EQUAL(status
, NT_STATUS_UNSUCCESSFUL
) &&
415 !NT_STATUS_IS_OK(state
.status
)) {
416 status
= state
.status
;