swrap: Wrap fopen to detect stale file descriptors.
[Samba.git] / source3 / printing / printer_list.c
blob9a9fa0b96228cdcae9d57373d5d26d7c9e5fc5f4
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 DBWRAP_FLAG_NONE);
45 return db;
48 bool printer_list_parent_init(void)
50 struct db_context *db;
53 * Open the tdb in the parent process (smbd) so that our
54 * CLEAR_IF_FIRST optimization in tdb_reopen_all can properly
55 * work.
58 db = get_printer_list_db();
59 if (db == NULL) {
60 DEBUG(1, ("could not open Printer List Database: %s\n",
61 strerror(errno)));
62 return false;
64 return true;
67 NTSTATUS printer_list_get_printer(TALLOC_CTX *mem_ctx,
68 const char *name,
69 const char **comment,
70 const char **location,
71 time_t *last_refresh)
73 struct db_context *db;
74 char *key;
75 TDB_DATA data;
76 uint32_t time_h, time_l;
77 char *nstr = NULL;
78 char *cstr = NULL;
79 char *lstr = NULL;
80 NTSTATUS status;
81 int ret;
83 db = get_printer_list_db();
84 if (db == NULL) {
85 return NT_STATUS_INTERNAL_DB_CORRUPTION;
88 key = talloc_asprintf(mem_ctx, PL_KEY_FORMAT, name);
89 if (!key) {
90 DEBUG(0, ("Failed to allocate key name!\n"));
91 return NT_STATUS_NO_MEMORY;
94 status = dbwrap_fetch_bystring_upper(db, key, key, &data);
95 if (!NT_STATUS_IS_OK(status)) {
96 DEBUG(6, ("Failed to fetch record! "
97 "The printer database is empty?\n"));
98 goto done;
101 ret = tdb_unpack(data.dptr, data.dsize,
102 PL_DATA_FORMAT,
103 &time_h, &time_l, &nstr, &cstr, &lstr);
104 if (ret == -1) {
105 DEBUG(1, ("Failed to un pack printer data"));
106 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
107 goto done;
110 if (last_refresh) {
111 *last_refresh = (time_t)(((uint64_t)time_h << 32) + time_l);
114 if (comment) {
115 *comment = talloc_strdup(mem_ctx, cstr);
116 if (!*comment) {
117 DEBUG(1, ("Failed to strdup comment!\n"));
118 status = NT_STATUS_NO_MEMORY;
119 goto done;
123 if (location) {
124 *location = talloc_strdup(mem_ctx, lstr);
125 if (*location == NULL) {
126 DEBUG(1, ("Failed to strdup location!\n"));
127 status = NT_STATUS_NO_MEMORY;
128 goto done;
132 status = NT_STATUS_OK;
134 done:
135 SAFE_FREE(nstr);
136 SAFE_FREE(cstr);
137 SAFE_FREE(lstr);
138 TALLOC_FREE(key);
139 return status;
142 NTSTATUS printer_list_set_printer(TALLOC_CTX *mem_ctx,
143 const char *name,
144 const char *comment,
145 const char *location,
146 time_t last_refresh)
148 struct db_context *db;
149 char *key;
150 TDB_DATA data;
151 uint64_t time_64;
152 uint32_t time_h, time_l;
153 NTSTATUS status;
154 int len;
156 db = get_printer_list_db();
157 if (db == NULL) {
158 return NT_STATUS_INTERNAL_DB_CORRUPTION;
161 key = talloc_asprintf(mem_ctx, PL_KEY_FORMAT, name);
162 if (!key) {
163 DEBUG(0, ("Failed to allocate key name!\n"));
164 return NT_STATUS_NO_MEMORY;
167 if (comment == NULL) {
168 comment = "";
171 if (location == NULL) {
172 location = "";
175 time_64 = last_refresh;
176 time_l = time_64 & 0xFFFFFFFFL;
177 time_h = time_64 >> 32;
179 len = tdb_pack(NULL, 0,
180 PL_DATA_FORMAT,
181 time_h,
182 time_l,
183 name,
184 comment,
185 location);
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,
197 time_h,
198 time_l,
199 name,
200 comment,
201 location);
203 status = dbwrap_store_bystring_upper(db, key, data, TDB_REPLACE);
205 done:
206 TALLOC_FREE(key);
207 return status;
210 NTSTATUS printer_list_get_last_refresh(time_t *last_refresh)
212 struct db_context *db;
213 TDB_DATA data;
214 uint32_t time_h, time_l;
215 NTSTATUS status;
216 int ret;
218 db = get_printer_list_db();
219 if (db == NULL) {
220 return NT_STATUS_INTERNAL_DB_CORRUPTION;
223 ZERO_STRUCT(data);
225 status = dbwrap_fetch_bystring(db, talloc_tos(), PL_TIMESTAMP_KEY, &data);
226 if (!NT_STATUS_IS_OK(status)) {
227 DEBUG(1, ("Failed to fetch record!\n"));
228 goto done;
231 ret = tdb_unpack(data.dptr, data.dsize,
232 PL_TSTAMP_FORMAT, &time_h, &time_l);
233 if (ret == -1) {
234 DEBUG(1, ("Failed to un pack printer data"));
235 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
236 goto done;
239 *last_refresh = (time_t)(((uint64_t)time_h << 32) + time_l);
240 status = NT_STATUS_OK;
242 done:
243 return status;
246 NTSTATUS printer_list_mark_reload(void)
248 struct db_context *db;
249 TDB_DATA data;
250 uint32_t time_h, time_l;
251 time_t now = time_mono(NULL);
252 NTSTATUS status;
253 int len;
255 db = get_printer_list_db();
256 if (db == NULL) {
257 return NT_STATUS_INTERNAL_DB_CORRUPTION;
260 time_l = ((uint64_t)now) & 0xFFFFFFFFL;
261 time_h = ((uint64_t)now) >> 32;
263 len = tdb_pack(NULL, 0, PL_TSTAMP_FORMAT, time_h, time_l);
265 data.dptr = talloc_array(talloc_tos(), uint8_t, len);
266 if (!data.dptr) {
267 DEBUG(0, ("Failed to allocate tdb data buffer!\n"));
268 status = NT_STATUS_NO_MEMORY;
269 goto done;
271 data.dsize = len;
273 len = tdb_pack(data.dptr, data.dsize,
274 PL_TSTAMP_FORMAT, time_h, time_l);
276 status = dbwrap_store_bystring(db, PL_TIMESTAMP_KEY,
277 data, TDB_REPLACE);
279 done:
280 TALLOC_FREE(data.dptr);
281 return status;
284 typedef int (printer_list_trv_fn_t)(struct db_record *, void *);
286 static NTSTATUS printer_list_traverse(printer_list_trv_fn_t *fn,
287 void *private_data,
288 bool read_only)
290 struct db_context *db;
291 NTSTATUS status;
293 db = get_printer_list_db();
294 if (db == NULL) {
295 return NT_STATUS_INTERNAL_DB_CORRUPTION;
298 if (read_only) {
299 status = dbwrap_traverse_read(db, fn, private_data, NULL);
300 } else {
301 status = dbwrap_traverse(db, fn, private_data, NULL);
304 return status;
307 struct printer_list_clean_state {
308 time_t last_refresh;
309 NTSTATUS status;
312 static int printer_list_clean_fn(struct db_record *rec, void *private_data)
314 struct printer_list_clean_state *state =
315 (struct printer_list_clean_state *)private_data;
316 uint32_t time_h, time_l;
317 time_t refresh;
318 char *name;
319 char *comment;
320 char *location;
321 int ret;
322 TDB_DATA key;
323 TDB_DATA value;
325 key = dbwrap_record_get_key(rec);
327 /* skip anything that does not contain PL_DATA_FORMAT data */
328 if (strncmp((char *)key.dptr,
329 PL_KEY_PREFIX, sizeof(PL_KEY_PREFIX)-1)) {
330 return 0;
333 value = dbwrap_record_get_value(rec);
335 ret = tdb_unpack(value.dptr, value.dsize,
336 PL_DATA_FORMAT, &time_h, &time_l, &name, &comment,
337 &location);
338 if (ret == -1) {
339 DEBUG(1, ("Failed to un pack printer data"));
340 state->status = NT_STATUS_INTERNAL_DB_CORRUPTION;
341 return -1;
344 SAFE_FREE(name);
345 SAFE_FREE(comment);
346 SAFE_FREE(location);
348 refresh = (time_t)(((uint64_t)time_h << 32) + time_l);
350 if (refresh < state->last_refresh) {
351 state->status = dbwrap_record_delete(rec);
352 if (!NT_STATUS_IS_OK(state->status)) {
353 return -1;
357 return 0;
360 NTSTATUS printer_list_clean_old(void)
362 struct printer_list_clean_state state;
363 NTSTATUS status;
365 status = printer_list_get_last_refresh(&state.last_refresh);
366 if (!NT_STATUS_IS_OK(status)) {
367 return status;
370 state.status = NT_STATUS_OK;
372 status = printer_list_traverse(printer_list_clean_fn, &state, false);
373 if (NT_STATUS_EQUAL(status, NT_STATUS_UNSUCCESSFUL) &&
374 !NT_STATUS_IS_OK(state.status)) {
375 status = state.status;
378 return status;
381 struct printer_list_exec_state {
382 void (*fn)(const char *, const char *, const char *, void *);
383 void *private_data;
384 NTSTATUS status;
387 static int printer_list_exec_fn(struct db_record *rec, void *private_data)
389 struct printer_list_exec_state *state =
390 (struct printer_list_exec_state *)private_data;
391 uint32_t time_h, time_l;
392 char *name;
393 char *comment;
394 char *location;
395 int ret;
396 TDB_DATA key;
397 TDB_DATA value;
399 key = dbwrap_record_get_key(rec);
401 /* always skip PL_TIMESTAMP_KEY key */
402 if (strequal((const char *)key.dptr, PL_TIMESTAMP_KEY)) {
403 return 0;
406 value = dbwrap_record_get_value(rec);
408 ret = tdb_unpack(value.dptr, value.dsize,
409 PL_DATA_FORMAT, &time_h, &time_l, &name, &comment,
410 &location);
411 if (ret == -1) {
412 DEBUG(1, ("Failed to un pack printer data"));
413 state->status = NT_STATUS_INTERNAL_DB_CORRUPTION;
414 return -1;
417 state->fn(name, comment, location, state->private_data);
419 SAFE_FREE(name);
420 SAFE_FREE(comment);
421 SAFE_FREE(location);
422 return 0;
425 NTSTATUS printer_list_read_run_fn(void (*fn)(const char *, const char *, const char *, void *),
426 void *private_data)
428 struct printer_list_exec_state state;
429 NTSTATUS status;
431 state.fn = fn;
432 state.private_data = private_data;
433 state.status = NT_STATUS_OK;
435 status = printer_list_traverse(printer_list_exec_fn, &state, true);
436 if (NT_STATUS_EQUAL(status, NT_STATUS_UNSUCCESSFUL) &&
437 !NT_STATUS_IS_OK(state.status)) {
438 status = state.status;
441 return status;