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_DB_NAME() lock_path("printer_list.tdb")
28 #define PL_KEY_PREFIX "PRINTERLIST/PRN/"
29 #define PL_KEY_FORMAT PL_KEY_PREFIX"%s"
30 #define PL_TIMESTAMP_KEY "PRINTERLIST/GLOBAL/LAST_REFRESH"
31 #define PL_DATA_FORMAT "ddPPP"
32 #define PL_TSTAMP_FORMAT "dd"
34 static struct db_context
*get_printer_list_db(void)
36 static struct db_context
*db
;
41 db
= db_open(NULL
, PL_DB_NAME(), 0,
42 TDB_DEFAULT
|TDB_CLEAR_IF_FIRST
|TDB_INCOMPATIBLE_HASH
,
43 O_RDWR
|O_CREAT
, 0644, DBWRAP_LOCK_ORDER_1
);
47 bool printer_list_parent_init(void)
49 struct db_context
*db
;
52 * Open the tdb in the parent process (smbd) so that our
53 * CLEAR_IF_FIRST optimization in tdb_reopen_all can properly
57 db
= get_printer_list_db();
59 DEBUG(1, ("could not open Printer List Database: %s\n",
66 NTSTATUS
printer_list_get_printer(TALLOC_CTX
*mem_ctx
,
69 const char **location
,
72 struct db_context
*db
;
75 uint32_t time_h
, time_l
;
82 db
= get_printer_list_db();
84 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
87 key
= talloc_asprintf(mem_ctx
, PL_KEY_FORMAT
, name
);
89 DEBUG(0, ("Failed to allocate key name!\n"));
90 return NT_STATUS_NO_MEMORY
;
93 status
= dbwrap_fetch_bystring_upper(db
, key
, key
, &data
);
94 if (!NT_STATUS_IS_OK(status
)) {
95 DEBUG(6, ("Failed to fetch record! "
96 "The printer database is empty?\n"));
100 ret
= tdb_unpack(data
.dptr
, data
.dsize
,
102 &time_h
, &time_l
, &nstr
, &cstr
, &lstr
);
104 DEBUG(1, ("Failed to un pack printer data"));
105 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
110 *last_refresh
= (time_t)(((uint64_t)time_h
<< 32) + time_l
);
114 *comment
= talloc_strdup(mem_ctx
, cstr
);
116 DEBUG(1, ("Failed to strdup comment!\n"));
117 status
= NT_STATUS_NO_MEMORY
;
123 *location
= talloc_strdup(mem_ctx
, lstr
);
124 if (*location
== NULL
) {
125 DEBUG(1, ("Failed to strdup location!\n"));
126 status
= NT_STATUS_NO_MEMORY
;
131 status
= NT_STATUS_OK
;
140 NTSTATUS
printer_list_set_printer(TALLOC_CTX
*mem_ctx
,
143 const char *location
,
146 struct db_context
*db
;
150 uint32_t time_h
, time_l
;
151 const char *str
= NULL
;
152 const char *str2
= NULL
;
156 db
= get_printer_list_db();
158 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
161 key
= talloc_asprintf(mem_ctx
, PL_KEY_FORMAT
, name
);
163 DEBUG(0, ("Failed to allocate key name!\n"));
164 return NT_STATUS_NO_MEMORY
;
180 time_64
= last_refresh
;
181 time_l
= time_64
& 0xFFFFFFFFL
;
182 time_h
= time_64
>> 32;
184 len
= tdb_pack(NULL
, 0, PL_DATA_FORMAT
, time_h
, time_l
, name
, str
, str2
);
186 data
.dptr
= talloc_array(key
, uint8_t, len
);
188 DEBUG(0, ("Failed to allocate tdb data buffer!\n"));
189 status
= NT_STATUS_NO_MEMORY
;
194 len
= tdb_pack(data
.dptr
, data
.dsize
,
195 PL_DATA_FORMAT
, time_h
, time_l
, name
, str
, str2
);
197 status
= dbwrap_store_bystring_upper(db
, key
, data
, TDB_REPLACE
);
204 NTSTATUS
printer_list_get_last_refresh(time_t *last_refresh
)
206 struct db_context
*db
;
208 uint32_t time_h
, time_l
;
212 db
= get_printer_list_db();
214 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
219 status
= dbwrap_fetch_bystring(db
, talloc_tos(), PL_TIMESTAMP_KEY
, &data
);
220 if (!NT_STATUS_IS_OK(status
)) {
221 DEBUG(1, ("Failed to fetch record!\n"));
225 ret
= tdb_unpack(data
.dptr
, data
.dsize
,
226 PL_TSTAMP_FORMAT
, &time_h
, &time_l
);
228 DEBUG(1, ("Failed to un pack printer data"));
229 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
233 *last_refresh
= (time_t)(((uint64_t)time_h
<< 32) + time_l
);
234 status
= NT_STATUS_OK
;
240 NTSTATUS
printer_list_mark_reload(void)
242 struct db_context
*db
;
244 uint32_t time_h
, time_l
;
245 time_t now
= time_mono(NULL
);
249 db
= get_printer_list_db();
251 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
254 time_l
= ((uint64_t)now
) & 0xFFFFFFFFL
;
255 time_h
= ((uint64_t)now
) >> 32;
257 len
= tdb_pack(NULL
, 0, PL_TSTAMP_FORMAT
, time_h
, time_l
);
259 data
.dptr
= talloc_array(talloc_tos(), uint8_t, len
);
261 DEBUG(0, ("Failed to allocate tdb data buffer!\n"));
262 status
= NT_STATUS_NO_MEMORY
;
267 len
= tdb_pack(data
.dptr
, data
.dsize
,
268 PL_TSTAMP_FORMAT
, time_h
, time_l
);
270 status
= dbwrap_store_bystring(db
, PL_TIMESTAMP_KEY
,
274 TALLOC_FREE(data
.dptr
);
278 typedef int (printer_list_trv_fn_t
)(struct db_record
*, void *);
280 static NTSTATUS
printer_list_traverse(printer_list_trv_fn_t
*fn
,
283 struct db_context
*db
;
286 db
= get_printer_list_db();
288 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
291 status
= dbwrap_traverse(db
, fn
, private_data
, NULL
);
296 struct printer_list_clean_state
{
301 static int printer_list_clean_fn(struct db_record
*rec
, void *private_data
)
303 struct printer_list_clean_state
*state
=
304 (struct printer_list_clean_state
*)private_data
;
305 uint32_t time_h
, time_l
;
314 key
= dbwrap_record_get_key(rec
);
316 /* skip anything that does not contain PL_DATA_FORMAT data */
317 if (strncmp((char *)key
.dptr
,
318 PL_KEY_PREFIX
, sizeof(PL_KEY_PREFIX
)-1)) {
322 value
= dbwrap_record_get_value(rec
);
324 ret
= tdb_unpack(value
.dptr
, value
.dsize
,
325 PL_DATA_FORMAT
, &time_h
, &time_l
, &name
, &comment
,
328 DEBUG(1, ("Failed to un pack printer data"));
329 state
->status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
337 refresh
= (time_t)(((uint64_t)time_h
<< 32) + time_l
);
339 if (refresh
< state
->last_refresh
) {
340 state
->status
= dbwrap_record_delete(rec
);
341 if (!NT_STATUS_IS_OK(state
->status
)) {
349 NTSTATUS
printer_list_clean_old(void)
351 struct printer_list_clean_state state
;
354 status
= printer_list_get_last_refresh(&state
.last_refresh
);
355 if (!NT_STATUS_IS_OK(status
)) {
359 state
.status
= NT_STATUS_OK
;
361 status
= printer_list_traverse(printer_list_clean_fn
, &state
);
362 if (NT_STATUS_EQUAL(status
, NT_STATUS_UNSUCCESSFUL
) &&
363 !NT_STATUS_IS_OK(state
.status
)) {
364 status
= state
.status
;
370 struct printer_list_exec_state
{
371 void (*fn
)(const char *, const char *, const char *, void *);
376 static int printer_list_exec_fn(struct db_record
*rec
, void *private_data
)
378 struct printer_list_exec_state
*state
=
379 (struct printer_list_exec_state
*)private_data
;
380 uint32_t time_h
, time_l
;
388 key
= dbwrap_record_get_key(rec
);
390 /* always skip PL_TIMESTAMP_KEY key */
391 if (strequal((const char *)key
.dptr
, PL_TIMESTAMP_KEY
)) {
395 value
= dbwrap_record_get_value(rec
);
397 ret
= tdb_unpack(value
.dptr
, value
.dsize
,
398 PL_DATA_FORMAT
, &time_h
, &time_l
, &name
, &comment
,
401 DEBUG(1, ("Failed to un pack printer data"));
402 state
->status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
406 state
->fn(name
, comment
, location
, state
->private_data
);
414 NTSTATUS
printer_list_run_fn(void (*fn
)(const char *, const char *, const char *, void *),
417 struct printer_list_exec_state state
;
421 state
.private_data
= private_data
;
422 state
.status
= NT_STATUS_OK
;
424 status
= printer_list_traverse(printer_list_exec_fn
, &state
);
425 if (NT_STATUS_EQUAL(status
, NT_STATUS_UNSUCCESSFUL
) &&
426 !NT_STATUS_IS_OK(state
.status
)) {
427 status
= state
.status
;