printing: traverse_read the printer list for share updates
[Samba.git] / source3 / printing / printer_list.c
blob2355e29b495cf9b890582ffea2d5d814ad971c57
1 /*
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/>.
20 #include "includes.h"
21 #include "system/filesys.h"
22 #include "dbwrap/dbwrap.h"
23 #include "dbwrap/dbwrap_open.h"
24 #include "util_tdb.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;
38 if (db != NULL) {
39 return 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);
44 return db;
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
54 * work.
57 db = get_printer_list_db();
58 if (db == NULL) {
59 DEBUG(1, ("could not open Printer List Database: %s\n",
60 strerror(errno)));
61 return false;
63 return true;
66 NTSTATUS printer_list_get_printer(TALLOC_CTX *mem_ctx,
67 const char *name,
68 const char **comment,
69 const char **location,
70 time_t *last_refresh)
72 struct db_context *db;
73 char *key;
74 TDB_DATA data;
75 uint32_t time_h, time_l;
76 char *nstr = NULL;
77 char *cstr = NULL;
78 char *lstr = NULL;
79 NTSTATUS status;
80 int ret;
82 db = get_printer_list_db();
83 if (db == NULL) {
84 return NT_STATUS_INTERNAL_DB_CORRUPTION;
87 key = talloc_asprintf(mem_ctx, PL_KEY_FORMAT, name);
88 if (!key) {
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"));
97 goto done;
100 ret = tdb_unpack(data.dptr, data.dsize,
101 PL_DATA_FORMAT,
102 &time_h, &time_l, &nstr, &cstr, &lstr);
103 if (ret == -1) {
104 DEBUG(1, ("Failed to un pack printer data"));
105 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
106 goto done;
109 if (last_refresh) {
110 *last_refresh = (time_t)(((uint64_t)time_h << 32) + time_l);
113 if (comment) {
114 *comment = talloc_strdup(mem_ctx, cstr);
115 if (!*comment) {
116 DEBUG(1, ("Failed to strdup comment!\n"));
117 status = NT_STATUS_NO_MEMORY;
118 goto done;
122 if (location) {
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;
127 goto done;
131 status = NT_STATUS_OK;
133 done:
134 SAFE_FREE(nstr);
135 SAFE_FREE(cstr);
136 SAFE_FREE(lstr);
137 TALLOC_FREE(key);
138 return status;
141 NTSTATUS printer_list_set_printer(TALLOC_CTX *mem_ctx,
142 const char *name,
143 const char *comment,
144 const char *location,
145 time_t last_refresh)
147 struct db_context *db;
148 char *key;
149 TDB_DATA data;
150 uint64_t time_64;
151 uint32_t time_h, time_l;
152 const char *str = NULL;
153 const char *str2 = NULL;
154 NTSTATUS status;
155 int len;
157 db = get_printer_list_db();
158 if (db == NULL) {
159 return NT_STATUS_INTERNAL_DB_CORRUPTION;
162 key = talloc_asprintf(mem_ctx, PL_KEY_FORMAT, name);
163 if (!key) {
164 DEBUG(0, ("Failed to allocate key name!\n"));
165 return NT_STATUS_NO_MEMORY;
168 if (comment) {
169 str = comment;
170 } else {
171 str = "";
174 if (location) {
175 str2 = location;
176 } else {
177 str2 = "";
181 time_64 = last_refresh;
182 time_l = time_64 & 0xFFFFFFFFL;
183 time_h = time_64 >> 32;
185 len = tdb_pack(NULL, 0, PL_DATA_FORMAT, time_h, time_l, name, str, str2);
187 data.dptr = talloc_array(key, uint8_t, len);
188 if (!data.dptr) {
189 DEBUG(0, ("Failed to allocate tdb data buffer!\n"));
190 status = NT_STATUS_NO_MEMORY;
191 goto done;
193 data.dsize = len;
195 len = tdb_pack(data.dptr, data.dsize,
196 PL_DATA_FORMAT, time_h, time_l, name, str, str2);
198 status = dbwrap_store_bystring_upper(db, key, data, TDB_REPLACE);
200 done:
201 TALLOC_FREE(key);
202 return status;
205 NTSTATUS printer_list_get_last_refresh(time_t *last_refresh)
207 struct db_context *db;
208 TDB_DATA data;
209 uint32_t time_h, time_l;
210 NTSTATUS status;
211 int ret;
213 db = get_printer_list_db();
214 if (db == NULL) {
215 return NT_STATUS_INTERNAL_DB_CORRUPTION;
218 ZERO_STRUCT(data);
220 status = dbwrap_fetch_bystring(db, talloc_tos(), PL_TIMESTAMP_KEY, &data);
221 if (!NT_STATUS_IS_OK(status)) {
222 DEBUG(1, ("Failed to fetch record!\n"));
223 goto done;
226 ret = tdb_unpack(data.dptr, data.dsize,
227 PL_TSTAMP_FORMAT, &time_h, &time_l);
228 if (ret == -1) {
229 DEBUG(1, ("Failed to un pack printer data"));
230 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
231 goto done;
234 *last_refresh = (time_t)(((uint64_t)time_h << 32) + time_l);
235 status = NT_STATUS_OK;
237 done:
238 return status;
241 NTSTATUS printer_list_mark_reload(void)
243 struct db_context *db;
244 TDB_DATA data;
245 uint32_t time_h, time_l;
246 time_t now = time_mono(NULL);
247 NTSTATUS status;
248 int len;
250 db = get_printer_list_db();
251 if (db == NULL) {
252 return NT_STATUS_INTERNAL_DB_CORRUPTION;
255 time_l = ((uint64_t)now) & 0xFFFFFFFFL;
256 time_h = ((uint64_t)now) >> 32;
258 len = tdb_pack(NULL, 0, PL_TSTAMP_FORMAT, time_h, time_l);
260 data.dptr = talloc_array(talloc_tos(), uint8_t, len);
261 if (!data.dptr) {
262 DEBUG(0, ("Failed to allocate tdb data buffer!\n"));
263 status = NT_STATUS_NO_MEMORY;
264 goto done;
266 data.dsize = len;
268 len = tdb_pack(data.dptr, data.dsize,
269 PL_TSTAMP_FORMAT, time_h, time_l);
271 status = dbwrap_store_bystring(db, PL_TIMESTAMP_KEY,
272 data, TDB_REPLACE);
274 done:
275 TALLOC_FREE(data.dptr);
276 return status;
279 typedef int (printer_list_trv_fn_t)(struct db_record *, void *);
281 static NTSTATUS printer_list_traverse(printer_list_trv_fn_t *fn,
282 void *private_data,
283 bool read_only)
285 struct db_context *db;
286 NTSTATUS status;
288 db = get_printer_list_db();
289 if (db == NULL) {
290 return NT_STATUS_INTERNAL_DB_CORRUPTION;
293 if (read_only) {
294 status = dbwrap_traverse_read(db, fn, private_data, NULL);
295 } else {
296 status = dbwrap_traverse(db, fn, private_data, NULL);
299 return status;
302 struct printer_list_clean_state {
303 time_t last_refresh;
304 NTSTATUS status;
307 static int printer_list_clean_fn(struct db_record *rec, void *private_data)
309 struct printer_list_clean_state *state =
310 (struct printer_list_clean_state *)private_data;
311 uint32_t time_h, time_l;
312 time_t refresh;
313 char *name;
314 char *comment;
315 char *location;
316 int ret;
317 TDB_DATA key;
318 TDB_DATA value;
320 key = dbwrap_record_get_key(rec);
322 /* skip anything that does not contain PL_DATA_FORMAT data */
323 if (strncmp((char *)key.dptr,
324 PL_KEY_PREFIX, sizeof(PL_KEY_PREFIX)-1)) {
325 return 0;
328 value = dbwrap_record_get_value(rec);
330 ret = tdb_unpack(value.dptr, value.dsize,
331 PL_DATA_FORMAT, &time_h, &time_l, &name, &comment,
332 &location);
333 if (ret == -1) {
334 DEBUG(1, ("Failed to un pack printer data"));
335 state->status = NT_STATUS_INTERNAL_DB_CORRUPTION;
336 return -1;
339 SAFE_FREE(name);
340 SAFE_FREE(comment);
341 SAFE_FREE(location);
343 refresh = (time_t)(((uint64_t)time_h << 32) + time_l);
345 if (refresh < state->last_refresh) {
346 state->status = dbwrap_record_delete(rec);
347 if (!NT_STATUS_IS_OK(state->status)) {
348 return -1;
352 return 0;
355 NTSTATUS printer_list_clean_old(void)
357 struct printer_list_clean_state state;
358 NTSTATUS status;
360 status = printer_list_get_last_refresh(&state.last_refresh);
361 if (!NT_STATUS_IS_OK(status)) {
362 return status;
365 state.status = NT_STATUS_OK;
367 status = printer_list_traverse(printer_list_clean_fn, &state, false);
368 if (NT_STATUS_EQUAL(status, NT_STATUS_UNSUCCESSFUL) &&
369 !NT_STATUS_IS_OK(state.status)) {
370 status = state.status;
373 return status;
376 struct printer_list_exec_state {
377 void (*fn)(const char *, const char *, const char *, void *);
378 void *private_data;
379 NTSTATUS status;
382 static int printer_list_exec_fn(struct db_record *rec, void *private_data)
384 struct printer_list_exec_state *state =
385 (struct printer_list_exec_state *)private_data;
386 uint32_t time_h, time_l;
387 char *name;
388 char *comment;
389 char *location;
390 int ret;
391 TDB_DATA key;
392 TDB_DATA value;
394 key = dbwrap_record_get_key(rec);
396 /* always skip PL_TIMESTAMP_KEY key */
397 if (strequal((const char *)key.dptr, PL_TIMESTAMP_KEY)) {
398 return 0;
401 value = dbwrap_record_get_value(rec);
403 ret = tdb_unpack(value.dptr, value.dsize,
404 PL_DATA_FORMAT, &time_h, &time_l, &name, &comment,
405 &location);
406 if (ret == -1) {
407 DEBUG(1, ("Failed to un pack printer data"));
408 state->status = NT_STATUS_INTERNAL_DB_CORRUPTION;
409 return -1;
412 state->fn(name, comment, location, state->private_data);
414 SAFE_FREE(name);
415 SAFE_FREE(comment);
416 SAFE_FREE(location);
417 return 0;
420 NTSTATUS printer_list_read_run_fn(void (*fn)(const char *, const char *, const char *, void *),
421 void *private_data)
423 struct printer_list_exec_state state;
424 NTSTATUS status;
426 state.fn = fn;
427 state.private_data = private_data;
428 state.status = NT_STATUS_OK;
430 status = printer_list_traverse(printer_list_exec_fn, &state, true);
431 if (NT_STATUS_EQUAL(status, NT_STATUS_UNSUCCESSFUL) &&
432 !NT_STATUS_IS_OK(state.status)) {
433 status = state.status;
436 return status;