s3: only include tdb headers where needed.
[samba.git] / source3 / printing / printer_list.c
1 /*
2    Unix SMB/CIFS implementation.
3    Share Database of available printers.
4    Copyright (C) Simo Sorce 2010
5
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.
10
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.
15
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/>.
18 */
19
20 #include "includes.h"
21 #include "system/filesys.h"
22 #include "dbwrap.h"
23 #include "util_tdb.h"
24 #include "printer_list.h"
25
26 #define PL_DB_NAME() lock_path("printer_list.tdb")
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 "ddPP"
31 #define PL_TSTAMP_FORMAT "dd"
32
33 static struct db_context *get_printer_list_db(void)
34 {
35         static struct db_context *db;
36
37         if (db != NULL) {
38                 return db;
39         }
40         db = db_open(NULL, PL_DB_NAME(), 0,
41                      TDB_DEFAULT|TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH,
42                      O_RDWR|O_CREAT, 0644);
43         return db;
44 }
45
46 bool printer_list_parent_init(void)
47 {
48         struct db_context *db;
49
50         /*
51          * Open the tdb in the parent process (smbd) so that our
52          * CLEAR_IF_FIRST optimization in tdb_reopen_all can properly
53          * work.
54          */
55
56         db = get_printer_list_db();
57         if (db == NULL) {
58                 DEBUG(1, ("could not open Printer List Database: %s\n",
59                           strerror(errno)));
60                 return false;
61         }
62         return true;
63 }
64
65 NTSTATUS printer_list_get_printer(TALLOC_CTX *mem_ctx,
66                                   const char *name,
67                                   const char **comment,
68                                   time_t *last_refresh)
69 {
70         struct db_context *db;
71         char *key;
72         TDB_DATA data;
73         uint32_t time_h, time_l;
74         char *nstr = NULL;
75         char *cstr = NULL;
76         NTSTATUS status;
77         int ret;
78
79         db = get_printer_list_db();
80         if (db == NULL) {
81                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
82         }
83
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;
88         }
89
90         data = dbwrap_fetch_bystring_upper(db, key, key);
91         if (data.dptr == NULL) {
92                 DEBUG(1, ("Failed to fetch record!\n"));
93                 status = NT_STATUS_NOT_FOUND;
94                 goto done;
95         }
96
97         ret = tdb_unpack(data.dptr, data.dsize,
98                          PL_DATA_FORMAT,
99                          &time_h, &time_l, &nstr, &cstr);
100         if (ret == -1) {
101                 DEBUG(1, ("Failed to un pack printer data"));
102                 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
103                 goto done;
104         }
105
106         if (last_refresh) {
107                 *last_refresh = (time_t)(((uint64_t)time_h << 32) + time_l);
108         }
109
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;
116                 }
117         }
118
119         status = NT_STATUS_OK;
120
121 done:
122         SAFE_FREE(nstr);
123         SAFE_FREE(cstr);
124         TALLOC_FREE(key);
125         return status;
126 }
127
128 NTSTATUS printer_list_set_printer(TALLOC_CTX *mem_ctx,
129                                   const char *name,
130                                   const char *comment,
131                                   time_t last_refresh)
132 {
133         struct db_context *db;
134         char *key;
135         TDB_DATA data;
136         uint64_t time_64;
137         uint32_t time_h, time_l;
138         const char *str = NULL;
139         NTSTATUS status;
140         int len;
141
142         db = get_printer_list_db();
143         if (db == NULL) {
144                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
145         }
146
147         key = talloc_asprintf(mem_ctx, PL_KEY_FORMAT, name);
148         if (!key) {
149                 DEBUG(0, ("Failed to allocate key name!\n"));
150                 return NT_STATUS_NO_MEMORY;
151         }
152
153         if (comment) {
154                 str = comment;
155         } else {
156                 str = "";
157         }
158
159         time_64 = last_refresh;
160         time_l = time_64 & 0xFFFFFFFFL;
161         time_h = time_64 >> 32;
162
163         len = tdb_pack(NULL, 0, PL_DATA_FORMAT, time_h, time_l, name, str);
164
165         data.dptr = talloc_array(key, uint8_t, len);
166         if (!data.dptr) {
167                 DEBUG(0, ("Failed to allocate tdb data buffer!\n"));
168                 status = NT_STATUS_NO_MEMORY;
169                 goto done;
170         }
171         data.dsize = len;
172
173         len = tdb_pack(data.dptr, data.dsize,
174                        PL_DATA_FORMAT, time_h, time_l, name, str);
175
176         status = dbwrap_store_bystring_upper(db, key, data, TDB_REPLACE);
177
178 done:
179         TALLOC_FREE(key);
180         return status;
181 }
182
183 NTSTATUS printer_list_get_last_refresh(time_t *last_refresh)
184 {
185         struct db_context *db;
186         TDB_DATA data;
187         uint32_t time_h, time_l;
188         NTSTATUS status;
189         int ret;
190
191         db = get_printer_list_db();
192         if (db == NULL) {
193                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
194         }
195
196         ZERO_STRUCT(data);
197
198         data = dbwrap_fetch_bystring(db, talloc_tos(), PL_TIMESTAMP_KEY);
199         if (data.dptr == NULL) {
200                 DEBUG(1, ("Failed to fetch record!\n"));
201                 status = NT_STATUS_NOT_FOUND;
202                 goto done;
203         }
204
205         ret = tdb_unpack(data.dptr, data.dsize,
206                          PL_TSTAMP_FORMAT, &time_h, &time_l);
207         if (ret == -1) {
208                 DEBUG(1, ("Failed to un pack printer data"));
209                 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
210                 goto done;
211         }
212
213         *last_refresh = (time_t)(((uint64_t)time_h << 32) + time_l);
214         status = NT_STATUS_OK;
215
216 done:
217         return status;
218 }
219
220 NTSTATUS printer_list_mark_reload(void)
221 {
222         struct db_context *db;
223         TDB_DATA data;
224         uint32_t time_h, time_l;
225         time_t now = time_mono(NULL);
226         NTSTATUS status;
227         int len;
228
229         db = get_printer_list_db();
230         if (db == NULL) {
231                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
232         }
233
234         time_l = ((uint64_t)now) & 0xFFFFFFFFL;
235         time_h = ((uint64_t)now) >> 32;
236
237         len = tdb_pack(NULL, 0, PL_TSTAMP_FORMAT, time_h, time_l);
238
239         data.dptr = talloc_array(talloc_tos(), uint8_t, len);
240         if (!data.dptr) {
241                 DEBUG(0, ("Failed to allocate tdb data buffer!\n"));
242                 status = NT_STATUS_NO_MEMORY;
243                 goto done;
244         }
245         data.dsize = len;
246
247         len = tdb_pack(data.dptr, data.dsize,
248                        PL_TSTAMP_FORMAT, time_h, time_l);
249
250         status = dbwrap_store_bystring(db, PL_TIMESTAMP_KEY,
251                                                 data, TDB_REPLACE);
252
253 done:
254         TALLOC_FREE(data.dptr);
255         return status;
256 }
257
258 typedef int (printer_list_trv_fn_t)(struct db_record *, void *);
259
260 static NTSTATUS printer_list_traverse(printer_list_trv_fn_t *fn,
261                                                 void *private_data)
262 {
263         struct db_context *db;
264         int ret;
265
266         db = get_printer_list_db();
267         if (db == NULL) {
268                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
269         }
270
271         ret = db->traverse(db, fn, private_data);
272         if (ret < 0) {
273                 return NT_STATUS_UNSUCCESSFUL;
274         }
275
276         return NT_STATUS_OK;
277 }
278
279 struct printer_list_clean_state {
280         time_t last_refresh;
281         NTSTATUS status;
282 };
283
284 static int printer_list_clean_fn(struct db_record *rec, void *private_data)
285 {
286         struct printer_list_clean_state *state =
287                         (struct printer_list_clean_state *)private_data;
288         uint32_t time_h, time_l;
289         time_t refresh;
290         char *name;
291         char *comment;
292         int ret;
293
294         /* skip anything that does not contain PL_DATA_FORMAT data */
295         if (strncmp((char *)rec->key.dptr,
296                     PL_KEY_PREFIX, sizeof(PL_KEY_PREFIX)-1)) {
297                 return 0;
298         }
299
300         ret = tdb_unpack(rec->value.dptr, rec->value.dsize,
301                          PL_DATA_FORMAT, &time_h, &time_l, &name, &comment);
302         if (ret == -1) {
303                 DEBUG(1, ("Failed to un pack printer data"));
304                 state->status = NT_STATUS_INTERNAL_DB_CORRUPTION;
305                 return -1;
306         }
307
308         SAFE_FREE(name);
309         SAFE_FREE(comment);
310
311         refresh = (time_t)(((uint64_t)time_h << 32) + time_l);
312
313         if (refresh < state->last_refresh) {
314                 state->status = rec->delete_rec(rec);
315                 if (!NT_STATUS_IS_OK(state->status)) {
316                         return -1;
317                 }
318         }
319
320         return 0;
321 }
322
323 NTSTATUS printer_list_clean_old(void)
324 {
325         struct printer_list_clean_state state;
326         NTSTATUS status;
327
328         status = printer_list_get_last_refresh(&state.last_refresh);
329         if (!NT_STATUS_IS_OK(status)) {
330                 return status;
331         }
332
333         state.status = NT_STATUS_OK;
334
335         status = printer_list_traverse(printer_list_clean_fn, &state);
336         if (NT_STATUS_EQUAL(status, NT_STATUS_UNSUCCESSFUL) &&
337             !NT_STATUS_IS_OK(state.status)) {
338                 status = state.status;
339         }
340
341         return status;
342 }
343
344 struct printer_list_exec_state {
345         void (*fn)(const char *, const char *, void *);
346         void *private_data;
347         NTSTATUS status;
348 };
349
350 static int printer_list_exec_fn(struct db_record *rec, void *private_data)
351 {
352         struct printer_list_exec_state *state =
353                         (struct printer_list_exec_state *)private_data;
354         uint32_t time_h, time_l;
355         char *name;
356         char *comment;
357         int ret;
358
359         /* always skip PL_TIMESTAMP_KEY key */
360         if (strequal((const char *)rec->key.dptr, PL_TIMESTAMP_KEY)) {
361                 return 0;
362         }
363
364         ret = tdb_unpack(rec->value.dptr, rec->value.dsize,
365                          PL_DATA_FORMAT, &time_h, &time_l, &name, &comment);
366         if (ret == -1) {
367                 DEBUG(1, ("Failed to un pack printer data"));
368                 state->status = NT_STATUS_INTERNAL_DB_CORRUPTION;
369                 return -1;
370         }
371
372         state->fn(name, comment, state->private_data);
373
374         SAFE_FREE(name);
375         SAFE_FREE(comment);
376         return 0;
377 }
378
379 NTSTATUS printer_list_run_fn(void (*fn)(const char *, const char *, void *),
380                              void *private_data)
381 {
382         struct printer_list_exec_state state;
383         NTSTATUS status;
384
385         state.fn = fn;
386         state.private_data = private_data;
387         state.status = NT_STATUS_OK;
388
389         status = printer_list_traverse(printer_list_exec_fn, &state);
390         if (NT_STATUS_EQUAL(status, NT_STATUS_UNSUCCESSFUL) &&
391             !NT_STATUS_IS_OK(state.status)) {
392                 status = state.status;
393         }
394
395         return status;
396 }