utils: Fix up 14a533680245
[samba4-gss.git] / source3 / printing / printer_list.c
blob7fd07ed1f34baa304beaaf47c933212fac1b1187
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 *printerlist_db;
35 static struct db_context *get_printer_list_db(void)
37 char *db_path;
39 if (printerlist_db != NULL) {
40 return printerlist_db;
43 db_path = lock_path(talloc_tos(), "printer_list.tdb");
44 if (db_path == NULL) {
45 return NULL;
48 printerlist_db = db_open(NULL,
49 db_path,
51 TDB_DEFAULT|TDB_INCOMPATIBLE_HASH,
52 O_RDWR|O_CREAT,
53 0644,
54 DBWRAP_LOCK_ORDER_1,
55 DBWRAP_FLAG_NONE);
56 TALLOC_FREE(db_path);
57 if (printerlist_db == NULL) {
58 DBG_ERR("Failed to open printer_list.tdb\n");
60 return printerlist_db;
63 NTSTATUS printer_list_get_printer(TALLOC_CTX *mem_ctx,
64 const char *name,
65 const char **comment,
66 const char **location,
67 time_t *last_refresh)
69 struct db_context *db;
70 char *key;
71 TDB_DATA data;
72 uint32_t time_h, time_l;
73 char *nstr = NULL;
74 char *cstr = NULL;
75 char *lstr = NULL;
76 NTSTATUS status;
77 int ret;
79 db = get_printer_list_db();
80 if (db == NULL) {
81 return NT_STATUS_INTERNAL_DB_CORRUPTION;
84 key = talloc_asprintf(mem_ctx, PL_KEY_FORMAT, name);
85 if (!key) {
86 DEBUG(0, ("Failed to allocate key name!\n"));
87 return NT_STATUS_NO_MEMORY;
90 status = dbwrap_fetch_bystring_upper(db, key, key, &data);
91 if (!NT_STATUS_IS_OK(status)) {
92 DEBUG(6, ("Failed to fetch record! "
93 "The printer database is empty?\n"));
94 goto done;
97 ret = tdb_unpack(data.dptr, data.dsize,
98 PL_DATA_FORMAT,
99 &time_h, &time_l, &nstr, &cstr, &lstr);
100 if (ret == -1) {
101 DEBUG(1, ("Failed to unpack printer data\n"));
102 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
103 goto done;
106 if (last_refresh) {
107 *last_refresh = (time_t)(((uint64_t)time_h << 32) + time_l);
110 if (comment) {
111 *comment = talloc_strdup(mem_ctx, cstr);
112 if (!*comment) {
113 DEBUG(1, ("Failed to strdup comment!\n"));
114 status = NT_STATUS_NO_MEMORY;
115 goto done;
119 if (location) {
120 *location = talloc_strdup(mem_ctx, lstr);
121 if (*location == NULL) {
122 DEBUG(1, ("Failed to strdup location!\n"));
123 status = NT_STATUS_NO_MEMORY;
124 goto done;
128 status = NT_STATUS_OK;
130 done:
131 SAFE_FREE(nstr);
132 SAFE_FREE(cstr);
133 SAFE_FREE(lstr);
134 TALLOC_FREE(key);
135 return status;
138 bool printer_list_printername_exists(const char *name)
140 struct db_context *db = get_printer_list_db();
141 char *key = NULL;
142 bool ok;
144 if (db == NULL) {
145 return false;
148 key = talloc_asprintf_strupper_m(
149 talloc_tos(), PL_KEY_FORMAT, name);
150 if (key == NULL) {
151 return false;
154 ok = dbwrap_exists(db, string_term_tdb_data(key));
155 TALLOC_FREE(key);
156 return ok;
159 NTSTATUS printer_list_set_printer(TALLOC_CTX *mem_ctx,
160 const char *name,
161 const char *comment,
162 const char *location,
163 time_t last_refresh)
165 struct db_context *db;
166 char *key;
167 TDB_DATA data;
168 uint64_t time_64;
169 uint32_t time_h, time_l;
170 NTSTATUS status;
171 int len;
173 db = get_printer_list_db();
174 if (db == NULL) {
175 return NT_STATUS_INTERNAL_DB_CORRUPTION;
178 key = talloc_asprintf(mem_ctx, PL_KEY_FORMAT, name);
179 if (!key) {
180 DEBUG(0, ("Failed to allocate key name!\n"));
181 return NT_STATUS_NO_MEMORY;
184 if (comment == NULL) {
185 comment = "";
188 if (location == NULL) {
189 location = "";
192 time_64 = last_refresh;
193 time_l = time_64 & 0xFFFFFFFFL;
194 time_h = time_64 >> 32;
196 len = tdb_pack(NULL, 0,
197 PL_DATA_FORMAT,
198 time_h,
199 time_l,
200 name,
201 comment,
202 location);
204 data.dptr = talloc_array(key, uint8_t, len);
205 if (!data.dptr) {
206 DEBUG(0, ("Failed to allocate tdb data buffer!\n"));
207 status = NT_STATUS_NO_MEMORY;
208 goto done;
210 data.dsize = len;
212 len = tdb_pack(data.dptr, data.dsize,
213 PL_DATA_FORMAT,
214 time_h,
215 time_l,
216 name,
217 comment,
218 location);
220 status = dbwrap_store_bystring_upper(db, key, data, TDB_REPLACE);
222 done:
223 TALLOC_FREE(key);
224 return status;
227 NTSTATUS printer_list_get_last_refresh(time_t *last_refresh)
229 struct db_context *db;
230 TDB_DATA data;
231 uint32_t time_h, time_l;
232 NTSTATUS status;
233 int ret;
235 db = get_printer_list_db();
236 if (db == NULL) {
237 return NT_STATUS_INTERNAL_DB_CORRUPTION;
240 ZERO_STRUCT(data);
242 status = dbwrap_fetch_bystring(db, talloc_tos(), PL_TIMESTAMP_KEY, &data);
243 if (!NT_STATUS_IS_OK(status)) {
244 DEBUG(1, ("Failed to fetch record!\n"));
245 goto done;
248 ret = tdb_unpack(data.dptr, data.dsize,
249 PL_TSTAMP_FORMAT, &time_h, &time_l);
250 TALLOC_FREE(data.dptr);
251 if (ret == -1) {
252 DEBUG(1, ("Failed to unpack printer data\n"));
253 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
254 goto done;
257 *last_refresh = (time_t)(((uint64_t)time_h << 32) + time_l);
258 status = NT_STATUS_OK;
260 done:
261 return status;
264 NTSTATUS printer_list_mark_reload(void)
266 struct db_context *db;
267 TDB_DATA data;
268 uint32_t time_h, time_l;
269 time_t now = time_mono(NULL);
270 NTSTATUS status;
271 int len;
273 db = get_printer_list_db();
274 if (db == NULL) {
275 return NT_STATUS_INTERNAL_DB_CORRUPTION;
278 time_l = ((uint64_t)now) & 0xFFFFFFFFL;
279 time_h = ((uint64_t)now) >> 32;
281 len = tdb_pack(NULL, 0, PL_TSTAMP_FORMAT, time_h, time_l);
283 data.dptr = talloc_array(talloc_tos(), uint8_t, len);
284 if (!data.dptr) {
285 DEBUG(0, ("Failed to allocate tdb data buffer!\n"));
286 status = NT_STATUS_NO_MEMORY;
287 goto done;
289 data.dsize = len;
291 len = tdb_pack(data.dptr, data.dsize,
292 PL_TSTAMP_FORMAT, time_h, time_l);
294 status = dbwrap_store_bystring(db, PL_TIMESTAMP_KEY,
295 data, TDB_REPLACE);
297 done:
298 TALLOC_FREE(data.dptr);
299 return status;
302 typedef int (printer_list_trv_fn_t)(struct db_record *, void *);
304 static NTSTATUS printer_list_traverse(printer_list_trv_fn_t *fn,
305 void *private_data,
306 bool read_only)
308 struct db_context *db;
309 NTSTATUS status;
311 db = get_printer_list_db();
312 if (db == NULL) {
313 return NT_STATUS_INTERNAL_DB_CORRUPTION;
316 if (read_only) {
317 status = dbwrap_traverse_read(db, fn, private_data, NULL);
318 } else {
319 status = dbwrap_traverse(db, fn, private_data, NULL);
322 return status;
325 struct printer_list_clean_state {
326 time_t last_refresh;
327 NTSTATUS status;
330 static int printer_list_clean_fn(struct db_record *rec, void *private_data)
332 struct printer_list_clean_state *state =
333 (struct printer_list_clean_state *)private_data;
334 uint32_t time_h, time_l;
335 time_t refresh;
336 char *name;
337 char *comment;
338 char *location;
339 int ret;
340 TDB_DATA key;
341 TDB_DATA value;
343 key = dbwrap_record_get_key(rec);
345 /* skip anything that does not contain PL_DATA_FORMAT data */
346 if (strncmp((char *)key.dptr,
347 PL_KEY_PREFIX, sizeof(PL_KEY_PREFIX)-1)) {
348 return 0;
351 value = dbwrap_record_get_value(rec);
353 ret = tdb_unpack(value.dptr, value.dsize,
354 PL_DATA_FORMAT, &time_h, &time_l, &name, &comment,
355 &location);
356 if (ret == -1) {
357 DEBUG(1, ("Failed to unpack printer data\n"));
358 state->status = NT_STATUS_INTERNAL_DB_CORRUPTION;
359 return -1;
362 SAFE_FREE(name);
363 SAFE_FREE(comment);
364 SAFE_FREE(location);
366 refresh = (time_t)(((uint64_t)time_h << 32) + time_l);
368 if (refresh < state->last_refresh) {
369 state->status = dbwrap_record_delete(rec);
370 if (!NT_STATUS_IS_OK(state->status)) {
371 return -1;
375 return 0;
378 NTSTATUS printer_list_clean_old(void)
380 struct printer_list_clean_state state;
381 NTSTATUS status;
383 status = printer_list_get_last_refresh(&state.last_refresh);
384 if (!NT_STATUS_IS_OK(status)) {
385 return status;
388 state.status = NT_STATUS_OK;
390 status = printer_list_traverse(printer_list_clean_fn, &state, false);
391 if (NT_STATUS_EQUAL(status, NT_STATUS_UNSUCCESSFUL) &&
392 !NT_STATUS_IS_OK(state.status)) {
393 status = state.status;
396 return status;
399 struct printer_list_exec_state {
400 void (*fn)(const char *, const char *, const char *, void *);
401 void *private_data;
402 NTSTATUS status;
405 static int printer_list_exec_fn(struct db_record *rec, void *private_data)
407 struct printer_list_exec_state *state =
408 (struct printer_list_exec_state *)private_data;
409 uint32_t time_h, time_l;
410 char *name;
411 char *comment;
412 char *location;
413 int ret;
414 TDB_DATA key;
415 TDB_DATA value;
417 key = dbwrap_record_get_key(rec);
419 /* always skip PL_TIMESTAMP_KEY key */
420 if (strequal((const char *)key.dptr, PL_TIMESTAMP_KEY)) {
421 return 0;
424 value = dbwrap_record_get_value(rec);
426 ret = tdb_unpack(value.dptr, value.dsize,
427 PL_DATA_FORMAT, &time_h, &time_l, &name, &comment,
428 &location);
429 if (ret == -1) {
430 DEBUG(1, ("Failed to unpack printer data\n"));
431 state->status = NT_STATUS_INTERNAL_DB_CORRUPTION;
432 return -1;
435 state->fn(name, comment, location, state->private_data);
437 SAFE_FREE(name);
438 SAFE_FREE(comment);
439 SAFE_FREE(location);
440 return 0;
443 NTSTATUS printer_list_read_run_fn(void (*fn)(const char *, const char *, const char *, void *),
444 void *private_data)
446 struct printer_list_exec_state state;
447 NTSTATUS status;
449 state.fn = fn;
450 state.private_data = private_data;
451 state.status = NT_STATUS_OK;
453 status = printer_list_traverse(printer_list_exec_fn, &state, true);
454 if (NT_STATUS_EQUAL(status, NT_STATUS_UNSUCCESSFUL) &&
455 !NT_STATUS_IS_OK(state.status)) {
456 status = state.status;
459 return status;