s4:kdc: split out a sdb_keys_free() helper function
[Samba.git] / source3 / printing / printer_list.c
blob4efcc2e2dd868c8581945b59df1931a7e12619e7
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_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;
36 char *db_path;
38 if (db != NULL) {
39 return db;
42 db_path = lock_path(talloc_tos(), "printer_list.tdb");
43 if (db_path == NULL) {
44 return 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,
50 DBWRAP_FLAG_NONE);
51 TALLOC_FREE(db_path);
52 return db;
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
62 * work.
65 db = get_printer_list_db();
66 if (db == NULL) {
67 DEBUG(1, ("could not open Printer List Database: %s\n",
68 strerror(errno)));
69 return false;
71 return true;
74 NTSTATUS printer_list_get_printer(TALLOC_CTX *mem_ctx,
75 const char *name,
76 const char **comment,
77 const char **location,
78 time_t *last_refresh)
80 struct db_context *db;
81 char *key;
82 TDB_DATA data;
83 uint32_t time_h, time_l;
84 char *nstr = NULL;
85 char *cstr = NULL;
86 char *lstr = NULL;
87 NTSTATUS status;
88 int ret;
90 db = get_printer_list_db();
91 if (db == NULL) {
92 return NT_STATUS_INTERNAL_DB_CORRUPTION;
95 key = talloc_asprintf(mem_ctx, PL_KEY_FORMAT, name);
96 if (!key) {
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"));
105 goto done;
108 ret = tdb_unpack(data.dptr, data.dsize,
109 PL_DATA_FORMAT,
110 &time_h, &time_l, &nstr, &cstr, &lstr);
111 if (ret == -1) {
112 DEBUG(1, ("Failed to un pack printer data"));
113 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
114 goto done;
117 if (last_refresh) {
118 *last_refresh = (time_t)(((uint64_t)time_h << 32) + time_l);
121 if (comment) {
122 *comment = talloc_strdup(mem_ctx, cstr);
123 if (!*comment) {
124 DEBUG(1, ("Failed to strdup comment!\n"));
125 status = NT_STATUS_NO_MEMORY;
126 goto done;
130 if (location) {
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;
135 goto done;
139 status = NT_STATUS_OK;
141 done:
142 SAFE_FREE(nstr);
143 SAFE_FREE(cstr);
144 SAFE_FREE(lstr);
145 TALLOC_FREE(key);
146 return status;
149 bool printer_list_printername_exists(const char *name)
151 struct db_context *db = get_printer_list_db();
152 char *key = NULL;
153 bool ok;
155 if (db == NULL) {
156 return false;
159 key = talloc_asprintf_strupper_m(
160 talloc_tos(), PL_KEY_FORMAT, name);
161 if (key == NULL) {
162 return false;
165 ok = dbwrap_exists(db, string_term_tdb_data(key));
166 TALLOC_FREE(key);
167 return ok;
170 NTSTATUS printer_list_set_printer(TALLOC_CTX *mem_ctx,
171 const char *name,
172 const char *comment,
173 const char *location,
174 time_t last_refresh)
176 struct db_context *db;
177 char *key;
178 TDB_DATA data;
179 uint64_t time_64;
180 uint32_t time_h, time_l;
181 NTSTATUS status;
182 int len;
184 db = get_printer_list_db();
185 if (db == NULL) {
186 return NT_STATUS_INTERNAL_DB_CORRUPTION;
189 key = talloc_asprintf(mem_ctx, PL_KEY_FORMAT, name);
190 if (!key) {
191 DEBUG(0, ("Failed to allocate key name!\n"));
192 return NT_STATUS_NO_MEMORY;
195 if (comment == NULL) {
196 comment = "";
199 if (location == NULL) {
200 location = "";
203 time_64 = last_refresh;
204 time_l = time_64 & 0xFFFFFFFFL;
205 time_h = time_64 >> 32;
207 len = tdb_pack(NULL, 0,
208 PL_DATA_FORMAT,
209 time_h,
210 time_l,
211 name,
212 comment,
213 location);
215 data.dptr = talloc_array(key, uint8_t, len);
216 if (!data.dptr) {
217 DEBUG(0, ("Failed to allocate tdb data buffer!\n"));
218 status = NT_STATUS_NO_MEMORY;
219 goto done;
221 data.dsize = len;
223 len = tdb_pack(data.dptr, data.dsize,
224 PL_DATA_FORMAT,
225 time_h,
226 time_l,
227 name,
228 comment,
229 location);
231 status = dbwrap_store_bystring_upper(db, key, data, TDB_REPLACE);
233 done:
234 TALLOC_FREE(key);
235 return status;
238 NTSTATUS printer_list_get_last_refresh(time_t *last_refresh)
240 struct db_context *db;
241 TDB_DATA data;
242 uint32_t time_h, time_l;
243 NTSTATUS status;
244 int ret;
246 db = get_printer_list_db();
247 if (db == NULL) {
248 return NT_STATUS_INTERNAL_DB_CORRUPTION;
251 ZERO_STRUCT(data);
253 status = dbwrap_fetch_bystring(db, talloc_tos(), PL_TIMESTAMP_KEY, &data);
254 if (!NT_STATUS_IS_OK(status)) {
255 DEBUG(1, ("Failed to fetch record!\n"));
256 goto done;
259 ret = tdb_unpack(data.dptr, data.dsize,
260 PL_TSTAMP_FORMAT, &time_h, &time_l);
261 TALLOC_FREE(data.dptr);
262 if (ret == -1) {
263 DEBUG(1, ("Failed to un pack printer data"));
264 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
265 goto done;
268 *last_refresh = (time_t)(((uint64_t)time_h << 32) + time_l);
269 status = NT_STATUS_OK;
271 done:
272 return status;
275 NTSTATUS printer_list_mark_reload(void)
277 struct db_context *db;
278 TDB_DATA data;
279 uint32_t time_h, time_l;
280 time_t now = time_mono(NULL);
281 NTSTATUS status;
282 int len;
284 db = get_printer_list_db();
285 if (db == NULL) {
286 return NT_STATUS_INTERNAL_DB_CORRUPTION;
289 time_l = ((uint64_t)now) & 0xFFFFFFFFL;
290 time_h = ((uint64_t)now) >> 32;
292 len = tdb_pack(NULL, 0, PL_TSTAMP_FORMAT, time_h, time_l);
294 data.dptr = talloc_array(talloc_tos(), uint8_t, len);
295 if (!data.dptr) {
296 DEBUG(0, ("Failed to allocate tdb data buffer!\n"));
297 status = NT_STATUS_NO_MEMORY;
298 goto done;
300 data.dsize = len;
302 len = tdb_pack(data.dptr, data.dsize,
303 PL_TSTAMP_FORMAT, time_h, time_l);
305 status = dbwrap_store_bystring(db, PL_TIMESTAMP_KEY,
306 data, TDB_REPLACE);
308 done:
309 TALLOC_FREE(data.dptr);
310 return status;
313 typedef int (printer_list_trv_fn_t)(struct db_record *, void *);
315 static NTSTATUS printer_list_traverse(printer_list_trv_fn_t *fn,
316 void *private_data,
317 bool read_only)
319 struct db_context *db;
320 NTSTATUS status;
322 db = get_printer_list_db();
323 if (db == NULL) {
324 return NT_STATUS_INTERNAL_DB_CORRUPTION;
327 if (read_only) {
328 status = dbwrap_traverse_read(db, fn, private_data, NULL);
329 } else {
330 status = dbwrap_traverse(db, fn, private_data, NULL);
333 return status;
336 struct printer_list_clean_state {
337 time_t last_refresh;
338 NTSTATUS status;
341 static int printer_list_clean_fn(struct db_record *rec, void *private_data)
343 struct printer_list_clean_state *state =
344 (struct printer_list_clean_state *)private_data;
345 uint32_t time_h, time_l;
346 time_t refresh;
347 char *name;
348 char *comment;
349 char *location;
350 int ret;
351 TDB_DATA key;
352 TDB_DATA value;
354 key = dbwrap_record_get_key(rec);
356 /* skip anything that does not contain PL_DATA_FORMAT data */
357 if (strncmp((char *)key.dptr,
358 PL_KEY_PREFIX, sizeof(PL_KEY_PREFIX)-1)) {
359 return 0;
362 value = dbwrap_record_get_value(rec);
364 ret = tdb_unpack(value.dptr, value.dsize,
365 PL_DATA_FORMAT, &time_h, &time_l, &name, &comment,
366 &location);
367 if (ret == -1) {
368 DEBUG(1, ("Failed to un pack printer data"));
369 state->status = NT_STATUS_INTERNAL_DB_CORRUPTION;
370 return -1;
373 SAFE_FREE(name);
374 SAFE_FREE(comment);
375 SAFE_FREE(location);
377 refresh = (time_t)(((uint64_t)time_h << 32) + time_l);
379 if (refresh < state->last_refresh) {
380 state->status = dbwrap_record_delete(rec);
381 if (!NT_STATUS_IS_OK(state->status)) {
382 return -1;
386 return 0;
389 NTSTATUS printer_list_clean_old(void)
391 struct printer_list_clean_state state;
392 NTSTATUS status;
394 status = printer_list_get_last_refresh(&state.last_refresh);
395 if (!NT_STATUS_IS_OK(status)) {
396 return status;
399 state.status = NT_STATUS_OK;
401 status = printer_list_traverse(printer_list_clean_fn, &state, false);
402 if (NT_STATUS_EQUAL(status, NT_STATUS_UNSUCCESSFUL) &&
403 !NT_STATUS_IS_OK(state.status)) {
404 status = state.status;
407 return status;
410 struct printer_list_exec_state {
411 void (*fn)(const char *, const char *, const char *, void *);
412 void *private_data;
413 NTSTATUS status;
416 static int printer_list_exec_fn(struct db_record *rec, void *private_data)
418 struct printer_list_exec_state *state =
419 (struct printer_list_exec_state *)private_data;
420 uint32_t time_h, time_l;
421 char *name;
422 char *comment;
423 char *location;
424 int ret;
425 TDB_DATA key;
426 TDB_DATA value;
428 key = dbwrap_record_get_key(rec);
430 /* always skip PL_TIMESTAMP_KEY key */
431 if (strequal((const char *)key.dptr, PL_TIMESTAMP_KEY)) {
432 return 0;
435 value = dbwrap_record_get_value(rec);
437 ret = tdb_unpack(value.dptr, value.dsize,
438 PL_DATA_FORMAT, &time_h, &time_l, &name, &comment,
439 &location);
440 if (ret == -1) {
441 DEBUG(1, ("Failed to un pack printer data"));
442 state->status = NT_STATUS_INTERNAL_DB_CORRUPTION;
443 return -1;
446 state->fn(name, comment, location, state->private_data);
448 SAFE_FREE(name);
449 SAFE_FREE(comment);
450 SAFE_FREE(location);
451 return 0;
454 NTSTATUS printer_list_read_run_fn(void (*fn)(const char *, const char *, const char *, void *),
455 void *private_data)
457 struct printer_list_exec_state state;
458 NTSTATUS status;
460 state.fn = fn;
461 state.private_data = private_data;
462 state.status = NT_STATUS_OK;
464 status = printer_list_traverse(printer_list_exec_fn, &state, true);
465 if (NT_STATUS_EQUAL(status, NT_STATUS_UNSUCCESSFUL) &&
466 !NT_STATUS_IS_OK(state.status)) {
467 status = state.status;
470 return status;